How to cache audio resources using CoreData

Fredric Cliver
2 min readJan 18, 2021
Photo by Maxwell Nelson on Unsplash

Add a new core data model into your workspace. and make a structure of the entities.

  1. I set the name of the entity as an ‘AudioCache’
  2. 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.
  3. 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

  1. Define the ‘coreDataContext’. this is a handler for whatever what you do for CoreData.
  2. NSFetchRequest and NSPredicate are to make a condition for retrieving exact one row as you want to get. Don’t forget the [entityName: “AudioCache”]
  3. getting audio from cache is defined on 22 line.
  4. saving audio as a Data type into the CoreData is defined on 28 to 31

--

--

Fredric Cliver

Majored in Physics, self-taught and worked in the IT industry as a Dev/Design/Planning for 11 years. And I had run my Startup for 3 years. I fancy a ☔️ 🇬🇧