How to cache audio resources using CoreData
2 min readJan 18, 2021
--
Add a new core data model into your workspace. and make a structure of the entities.
- I set the name of the entity as an ‘AudioCache’
- added two attributes ‘remoteUrl’ and ‘data’. I will use the first one as a key to the data row. and the data is the contents.
- I set ‘Allows External Storage’ if this is checked, iOS will determine the location based on the size of data. It would be a 1MB for the huddle. It is good for performance.
AppDelegate
add this the parts into your AppDelegate.
ViewController
- Define the ‘coreDataContext’. this is a handler for whatever what you do for CoreData.
- NSFetchRequest and NSPredicate are to make a condition for retrieving exact one row as you want to get. Don’t forget the [entityName: “AudioCache”]
- getting audio from cache is defined on 22 line.
- saving audio as a Data type into the CoreData is defined on 28 to 31