How to cache audio resources using CoreData

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fredric Cliver

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 ☔️ 🇬🇧