Core Data and iOS 7: Different Behavior of Persistent Store

Core Data and iOS 7: Different behavior of persistent store

Yes, Apple have changed the default journal mode to WAL for iOS7.
You can specify the journal mode by adding the NSSQLitePragmasOption to the options when calling addPersistentStoreWithType:configuration:url:options:error. E.g. to set the previous default mode of DELETE:

NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };

In my experience WAL gives better performance, but also see this post:

iOS CoreData - are there any disadvantages to enabling sqlite WAL / Write-Ahead Logging

Default Method of Built-in Persistent Storage Type Of Core Data In Xcode

Well, NSPersistentStore is an abstract base class. One of its four subclasses must be deliberately created in code. So, there is not really a default store type.

There is, however some template code which gets generated when you choose one of the New Project or New Target templates in Xcode. In the current version (Xcode 10), when you switch on the Use Core Data checkbox, you get in the AppDelegate implementation a lazy var persistentContainer property which provides a singleton NSPersistentContainer object. This is by default the store you will use in the new target. But what store type is that? Well, the persistent store(s) of a NSPersistentContainer are specified in its persistentStoreDescriptions array property. By default, this array contains one persistent store of type SQLite, backed by a file in the Application Support folder of your app's container. This is, in a round-about way, the default store type you asked about.

The procedure to change this store type is explained in the Discussion section of the documentation of NSPersistentContainer.persistentstoredescriptions.

So you see the default is actually in the Xcode Project Templates. To get what you want, you can place the code you wrote after reading that documentation into a new Project Template and add it to your ~/Library. You can either override one of the default Project Templates, or create your own with a new name. A basic example is given in this blog post by Jake Craige. If that is not enough for you, Keith Harrison has published a quite thorough reverse engineering of Xcode Project Templates.

CoreData: persistent and temporary storage

You need to create 2 separate Core Data 'stacks' - i.e. 2 different models (assuming the stored data is different in each), persistent stores, persistent store coordinators and managed object contexts. Both stacks will save the model to a file, but your temporary file should save into NSTemporaryDirectory (or perhaps better a cache directory) whereas your permanent file should be saved into NSHomeDirectory.

Other than that the usage of Core Data is nothing special. You just need to use the appropriate managed object context for the data you are saving / retrieving.

If you wanted to move any objects from one store to the other you would need to write the code to do that (i.e. get the object, create a new object in the other store and then copy each attribute across - use dictionaryWithValuesForKeys: and setValuesForKeysWithDictionary:).

Core Data: move object from one persistent store to another

how can I tell my managed object so copy itself into the other store?

You can't, not directly anyway. You'll have to do something like:

  • For each object in the origin data store,
    • Create a new object in the target store with the same entity type
    • Assign the new object's attributes to the same values as the original object
  • Once you're done creating new objects, do a second pass to set up any relationships.

The relationships need to be done separately, because all of the objects in a relationship need to exist before you can create the relationship.



Related Topics



Leave a reply



Submit