Core Data custom metadata not saving for iCloud store

Viewed 532

I am trying to save some custom metadata to an iCloud-enabled Core Data SQLite store. The documentation appears to say that in addition to the metadata it generates, I should be able to set my own custom metadata - similar to NSUserPreferences, but attached to my Core Data store. It seems like it should be simple:

NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[moc setPersistentStoreCoordinator:psc];

NSMutableDictionary* metadata = [[psc metadataForPersistentStore:store] mutableCopy];
metadata[@"testKey"] = @"testValue";
[psc setMetadata:metadata forPersistentStore:store];
[moc save:&error];

So after this runs my custom metadata key/value is visible in the store's metadata field. However, when I restart the app the metadata I added is no longer there (but the iCloud metadata is there). Note that this works correctly when using a regular non-iCloud store so it appears to be iCloud specific.

Additional information:

So lets say I get the metadata, add my custom fields and then save it using the static methods:

[NSPersistentStoreCoordinator setMetadata:metadata forPersistentStoreOfType:NSSQLiteStoreType URL:self.coreDataController.iCloudStore.URL error:&error];
NSDictionary* reviewMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:self.coreDataController.iCloudStore.URL error:&error];

The reviewMetadata variable contains the metadata with my added fields. However once I restart the app (and consequently reload the store) it's back to the way it was. I even tried setting a breakpoint and killing the app right after executing the above statements and when I restart the app my custom metadata is still gone.

Any ideas? Am I using metadata incorrectly? Does this not work with iCloud-enabled stores?

2 Answers
Related