Recently I watched Developing iOS Apps with Swift by Stanford U, which was released in iTunes U on Jan 24, 2017. Even this course is for beginners, I still found many new concepts about iOS 10. One of the most interesting thing is about CoreData.
I tried to incorporate the new strategy strongly recommend by Paul Hegarty (instructor) in my App. Soon I realized that many new classes or methods by this strategy are only for devices in iOS 10.*. My app is for pre iOS 10 as well. After some consideration, I decided to use this strategy anyway. For pre iOS 10 cases, I'll continue to use UIManagedDocument
to load CoreData database.
My app uses a customized store URL for loading data from CoreData database. The database is located in app's Documents
folder. I added the new strategy into my project and tried to use the same URL to load database. To my surprise, my previous database are gone when I run the project in Simulator.
After deep investigation into where the SQLite database is, I found that the URL used by UIManagedDocument
is actually pointing to the SQLite database two levels of directories down.
In above example, the URL by UIManagedDocument
is defaultDatabase
, but the SQLite database is persistentStore
, under the directory of defaultDatabase/storeContent
.
However, the URL used by the new strategy, i.e., NSPersistentContainer
, is the URL directly pointing to the SQLite database, persistentStore
.
Actually, the first time I tried to load my database by previous URL caused my project crashing. As I discovered above, the URL pointing to defaultDatabase
, which is a folder name.
This presents an issue for my app. Some users's device may be pre iOS 10. If they update their device to iOS 10.*, my app has to be able to adjust the previous URL to the correct database, so that users would not lose their data.
After some changes, my app can deal with this transition smoothly without losing their data.
This is a very good experience, incorporating new and efficient strategies into my app and dealing with prior iOS 10 devices.
References
- StackOverflow QA: How to set a custom store URL for NSPersistentContainer
- Stanford U Couse: Developing iOS App with Swift
- Apple Developer's site: WWDC 2016 Session 242 What’s New in Core Data
0 comments:
Post a Comment