开发者

Add Core Data Index to certain Attributes via migration

For performance reasons, i want to set the Indexed Attribute to some of my entities. I created a new core data model version to perform the changes. Core Data detects the changes and migrates my model to the new version, however, NO INDEXES ARE GENERATED.

If I recreate the database from scratch, the indexes are there. I checked with SQLite Browser both on the iPhone and on the Simulator. The problem only occurs if a database in the prior format is already there.

Is there a way to manually add the indexes? Write some sql for that? Or am I missing something? I did already some more critical migrations, no prob开发者_高级运维lems there. But those missing indexes are bugging me.

Thanks for helping!


I had the same problem.

According to the Core Data Model Versioning and Data Migration Programming Guide:

Core Data’s perspective on versioning is that it is only interested in features of the model that affect persistence.

Simply adding an index to an existing model property does not automatically trigger a migration since Core Data does not see your two model schemas as being different (an index does not affect persistence).

You can, however, cause Core Data to see your model as being changed by adding a Version Hash Modifier to your newly indexed attribute. This triggered lightweight migration to update my existing databases.


What migration strategy did you use?

In XCode you can generate a mapping model, and view the changes that will occur, including indexing.

I suggest simply adding the mapping model, verifying the index changes are specified, and do the automatic lightweight migration:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];
NSPersistentStore *migratedStore = [persistentStoreCoordinator addPersistentStoreWithType:nil configuration:nil URL:storeURL options:options error:&error];
migrationWasSuccessful = (migratedStore != nil);

The Persistent store coordinator will auto-discover the mapping model, and use it to perform the migration. It sounds like the run-time inferred model is not sensitive to applying indexes to entity properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜