WWDC 2015

What's new in Core Data

use NSFetchRequest to

  • Find the data
  • Batch size of data
  • Relationship prefetching (doing in memory)

Core data handles multi-writing conflicts ,migration and persistent store vs. in-memory

New API

var haspersistentChangedValues: Bool { get } in NSManagedObject marks the object are dirty and prevent false positive


func objectIDsForRelationshipNamed(key: String) -> [NSManagedObjectID] for fetching 1 to many objects


refreshAllObjects() for

  1. all objects in a context,
  2. preserves unsaved changes
  3. managedObject ref. are still valid
  4. useful to break retain cycles

class func mergeChangesFromRemoteContextSave(changeNotificationData:[NSObject : AnyObject], intoContexts contexts: [NSManagedObjectContext])

  • for better tracking changes in different coordinators
  • fetch latest row data
  • handling ordering with nested contexts

var shouldDeleteInaccessibleFaults: Bool

  • used to delete the reference that pointing invalid objects
  • Default to true
  • Not affecting to the api with error parameters(so still have error for handling)
  • missing data treated as NULL/nil/0

We often need to remove the underlying database and create a new one for latest data. By doing so, that leaves many issues such as corrupting the files and bad access to the live connections

func destroyPersistentStoreAtURL(url: NSURL, withType storeType: String,options: [NSObject : AnyObject]?) throws
  • used to remove database in a safer way
  • honors locking protocols
  • handles details reconfig emptied files
    • Journal mode, page size
    • Need to pass same options to addToPersistentStore
    • switching Journal mode may result in deadlock

At the same we may need to replace one db with other

func replacePersistentStoreAtURL(destinationURL: NSURL, destinationOptions:[NSObject : AnyObject]?, withPersistentStoreFromURL sourceURL: NSURL, sourceOptions: [NSObject : AnyObject]?, storeType: String) throws
  • if the destination does not exists, this will do a copy to the destination

Remove duplication

In Xcode 7, we can define what property of a model can be used as unique identifier.

Objection deletion

For current situation, if we need to delete some objects, we have to do the following 4 steps

  1. fetching the objects we wanted to delete
  2. mark each of the objects to delete
  3. save the changes
  4. repeat for more objects to delete

NSBatchDeleteRequest is new api for us to not to pre-fetching the objects with focus on deleting

  • on one entiy,
  • one or more stores and
  • supporting predicates with sort descriptors and offsets.

returns

  • result of success/ fail
  • count of objects deleted
  • object IDs of objects deleted

and limitations are:

  • not reflected in the context
  • not all validation rules are enforced
  • no object notification

Models Change

we often update the models as app versions advance. Migration is needed to perform for differnt versions

Problem

  1. Iterating all models is cumbersome
  2. forgetting to deploy model versions is dangerous
  3. Automatic lightweight migrations should “Just Work”

in iOS 9

Model caching as last-ditch effort to recover

  1. NSManagedObjectModel copied to store
  2. Auto updates to existing stores
  3. lightweight migrations fetch the model from the store

limitation

  • only SQLite store
  • cached models is not available to explicit migrations (as developers should prepare and know how to do the migration anyway)

API Changes

  • nullability in API
  • __kindof
  • generated subclass use generics for 1 to many relationships

no need to update the header for adding new properties. Instead, editing Subclass+NSMAngedProperties.h or Subclass+NSMangedProperties.swift for adding those new properties.

init(concurrencyType:) is the designated initializer

Related session: What's new in Core Data on iOS WWDC 2011 or NSManagedObjectContext Documentation

Performance

Use instrutment to measure the performance and find the data that should not prefetch

use -com.apple.CoreData.SQLDebug 1 for show query SQL and running time

then use EXPLAIN QUERY PLAN [sql] to see the steps of the execution of sql