small medium large xlarge

01 Dec 2013, 07:11
David Wilson (11 posts)

I’m using Xcode 5.1, developing for Mavericks.

I’ve converted my Mac OS application to use Core Data and it has been going well. Next on my list is to add iCloud support. So I added the entitlement, and added the extra code for the persistent store so that iCloud is supported.

So the code looks like thus…

	dispatch_queue_t queue = NULL;
	queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
	dispatch_async(queue, ^{	
		NSMutableDictionary * options = [[NSMutableDictionary alloc] init];
		[options setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
		[options setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
		NSFileManager *fileManager = [NSFileManager defaultManager];
		NSURL * cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
		if (cloudURL)
			NSLog(@"%@ iCloud enabled: %@\n\n", NSStringFromSelector(_cmd), cloudURL);
			cloudURL = [cloudURL URLByAppendingPathComponent:@"TrafficCamNZ"];
			[options setValue:[[NSBundle mainBundle] bundleIdentifier] forKey:NSPersistentStoreUbiquitousContentNameKey];
			[options setValue:cloudURL forKey:NSPersistentStoreUbiquitousContentURLKey];
		} else
			NSLog(@"%@ iCloud is not enabled\n\n", NSStringFromSelector(_cmd));
		NSURL *storeURL = nil;
		NSArray *directoryArray = [fileManager URLsForDirectory:NSApplicationSupportDirectory
		storeURL = [directoryArray lastObject];
		NSString * applicationIdentifier = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleIdentifier"];
		storeURL = [storeURL URLByAppendingPathComponent:applicationIdentifier];
		storeURL = [storeURL URLByAppendingPathComponent:@"TrafficCamNZ.sqlite"];
		NSError *error = nil;
		NSPersistentStore *store = nil;
		store = [psc addPersistentStoreWithType:NSSQLiteStoreType
		if (!store) {
			NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:@"Error adding persistent store to coordinator %@\n%@", [error localizedDescription], [error userInfo]]);
			[[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__
																	file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding]
															 description:@"Error adding persistent store to coordinator"];
			//Present a user facing error
#pragma mark TODO: Present a user facing error

And when it runs… I get the following error…

2013-12-01 13:41:22.060 TrafficCamNZ[82471:1307] initializeCoreDataStack iCloud enabled: file:///Users/dgwilson/Library/Mobile%20Documents/ZNVB52L2P6~nz~co~catch22~TrafficCamNZ/

2013-12-01 13:41:22.082 TrafficCamNZ[82471:1307] -[PFUbiquitySetupAssistant validateOptionsWithError:](332): CoreData: Ubiquity:  Invalid option: the value for NSPersistentStoreUbiquitousContentNameKey should not contain periods:
2013-12-01 13:41:22.139 TrafficCamNZ[82471:1307] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](760): CoreData: Ubiquity:
Using local storage: 1

So this means there’s an issue using the CFBundleIdentifier…

What should I do now?

  • David
09 Dec 2013, 20:21
Marcus S. Zarra (282 posts)

This was changed recently (Mavericks). Fortunately for you, since you are just now adding iCloud, the impact is minimal.

You need to change the following line of code:

[options setValue:[[NSBundle mainBundle] bundleIdentifier] forKey:NSPersistentStoreUbiquitousContentNameKey];

To something else. What is that something else? I would recommend something descriptive to your application. I have been using class name like structures recently. So I would change it to be the name of your app perhaps or simply “DataStorage”.

The name is unique to your application so the actual value is not important as long as it is understood by you.

03 Feb 2014, 10:18
David Wilson (11 posts)

Wonderful. This change has worked.

[Wish these forums would e-mail when a post is made… - didn’t return to check here for 2 months!].

I’m very happy this is working… I can complete testing and changes to move forward for AppStore… Thank you.

  • David
23 Mar 2014, 16:18
Paul Linsay (2 posts)

While updating an app I came across this warning too. If I remove the period the old content gets lost, which I don’t want to do to my customers. What’s a good way to deal with this problem? Is leaving it untouched acceptable since it’s only a warning?



01 Jul 2014, 20:18
Marcus S. Zarra (282 posts)

For now yes, I would leave it alone. Just make sure you test for the issue with each version of your app in case Apple changes things. When/if they do then you are looking at a potential migration situation to get the data over to a new key.

14 May 2015, 13:35
Paul Linsay (2 posts)

The day has arrived when a period in the key can no longer contain a period. How do I go about migrating to a different key without losing my customers’ data? Any advice would be appreciated.

You must be logged in to comment