WWDC 2016 Spotlight: Core Data
The launch of iOS 10 this fall means advancements for its tools and frameworks, including Core Data. Apple recently unveiled some important updates to Core Data that focus on ease of use, safety, and control. Here are some initial thoughts about those changes and how they’ll impact developers.
Making Core Data More Accessible
The NSPersistentContainer class provides a nice full-featured Core Data stack, which is very simple to set up and use. It provides a viewContext, which, as the name suggests, is meant to be used by view controllers to manage “visible” model objects. It makes performing operations on a background context easy with the method performBackgroundTask(_,:), which gives developers the managed object context needed to perform those operations. This should significantly remove the barrier to entry for Core Data. It will also make some common operations a lot faster to write by hiding some of the complexities from developers, especially with respect to background context operations.
Preventing Concurrency Issues
Regarding fetching, the new NSFetchRequest class should make using Core Data both easier and safer; it uses generics to create objects of a specific class and has a built-in mechanism to prevent executing a fetch outside of a performBlock() or performBlockAndWait() closure. This should further reduce concurrency issues with Core Data.
Giving Developers More Control
On the more advanced side of Core Data, developers will now be able to lock the results of a fetch on a context to a specific version of the data. This means that unless the main context is explicitly saved or merged, its data could remain unchanged and tagged to a specific version of the data all while changes are happening in the background. This will give developers more control over when their view context actually changes.
Reading Data from the Store
On the performance front, Core Data now allows multiple simultaneous reads and a single write to the data without locking the store and blocking the contexts. In prior versions of iOS, when a context was reading from the store all other contexts had to wait to perform their own read or write. This is longer the case. Now multiple contexts can read data from the store simultaneously. Naturally, only one context at a time can write to the store at a time. In fact, the lock which used to occur at the NSPersistentStoreCoordinator is now moved to the SQLite database. This means that some serialization still occurs whenever the SQLite store is hit. In the presentation at WWDC, Apple stated that a net result of this will be a more responsive UI where the view context is able to fetch data while a background context writes data to disk. We are looking forward to this.
More Core Data Resources
These Core Data developments are exciting as they should make the framework more approachable and performant. If you’d like to know more about getting started with Core Data, check out these resources from our team:
For more insights on design and development, subscribe to BPXL Craft and follow Black Pixel on Twitter.