How to cache audio resources using CoreData

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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to insert a custom Watch Face using a WatchOS app

Become a Lucrative iOS App Developer in 2020: An Ultimate Guide

Understanding Property Wrappers in Swift

An Introduction to Property Wrappers in Swift

Solving modularised iOS project challenges by adopting project generation

IOS-Swift language learning notes: Your First iOS and UIKit App tutorial from Ray Wenderlich

Whats New in SwiftUI 3

#2 Xcode Assignment : UI Element — Segmented Control/Slider/Switch/Activity Indicator…

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

More from Medium

Preventing merge conflicts with XcodeGen for your xcode project using xcodeGen and project.yml file

Swift: easy way to add tap gesture on a View

SplitView controller in Xcode 12 and Swift 5

How to compare strings based on the number of vowels it has in Swift?