In Apple’s new tvOS, data saved in core data is not persisted but is only cached. The tvOS may delete the data stored in core data when space is low.
Local Storage for Your App Is Limited
The maximum size of an Apple TV app is limited to 200 MB. Moreover, your app can only access 500 KB of persistent storage that is local to the device (using the NSUserDefaults class). Outside of this limited local storage, all other data must be purgeable by the operating system when space is low. You have a few options for managing these resources:
Your app can store and retrieve user data in iCloud.
Your app can download the data it needs into its cache directory. Downloaded data is not deleted while the app is running. However, when space is low and your app is not running, this data may be deleted. Do not use the entire cache space as this can cause unpredictable results.
Your app can package read-only assets using on-demand resources. Then, at runtime, your app requests the resources it needs, and the operating system automatically downloads and manages those resources. Knowing how and when to load new assets while keeping your users engaged is critical to creating a successful app. For information on on-demand resources, see On-Demand Resources Guide
This means that every app developed for the new Apple TV must be able to store data in iCloud and retrieve it in a way that provides a great customer experience.
If you have an existing iOS app that already uses core data and you want to be able to continue using it in tvOS with the knowledge that you may need to recreate the data stored in core data then read on.
First you need to create a new target for tvOS.
Select your project. File, New, Target ..
Select tvOS, Application, Single View Application (or another template as appropriate for your project), Next.
Enter your Product Name and ensure “Use Core Data” is checked. Press Finish.
From within the new tvOS App target, delete the newly created .xcdatamodeld file
Open up the tvOS AppDelegate file and locate the following line within the managedObjectModel declaration:
let modelURL = NSBundle.mainBundle().URLForResource(“[YOUR_TV_OS_APP_NAME]”, withExtension: “momd”)!
Replace the default url string with the name of your existing iOS app target. Or if you have moved your xcdatamodeld file somewhere else use what is in your existing iOS AppDelegate file.
Open you existing iOS xcdatamodeld file and add the new tvOS app to the “Target Membership” section.
Don’t forget to check if the database still contains data and repopulate it as necessary, every time your app is launched.
- Xcode Version 7.2.1
- tvOS 9.1.1
- Swift 2.1.1
Did you find this post helpful? Support independent development and improve your sleep by downloading White Noise and Deep Sleep Sounds free from the App Store.
Keyword level attribution for Apple Search Ads. Optimize your bids and increase your revenue. Sign up for free at kitemetrics.com.