Re: Adventures in core-data, iCloud, and sandboxing
Re: Adventures in core-data, iCloud, and sandboxing
- Subject: Re: Adventures in core-data, iCloud, and sandboxing
- From: Martin Hewitson <email@hidden>
- Date: Fri, 04 Nov 2011 17:24:19 +0100
On 4, Nov, 2011, at 03:06 PM, Roland King wrote:
>
> On Nov 4, 2011, at 1:41 PM, Martin Hewitson wrote:
>
>>
>> On 4, Nov, 2011, at 02:01 AM, Roland King wrote:
>>
>>>
>>>
>>>
>>>
>>>> So, can I conclude from this that iCloud and core-data only works with SQL store? The WWDC video hints at this, but was not explicit.
>>>>
>>>
>>> Not so. I have a core data app running using icloud and an XML store. This is ios by the way and the store is not incremental, it's just being treated as a blob which is fully synced each time but it's small so that's ok.
>>>
>>> Definitely if you want the incremental log style store you have to use SQL but in general core data in iCloud will let you use whatever you like.
>>
>> I hadn't realised that I had made a choice. How does one choose an incremental store as opposed to a blob? Any pointers how you got your core-data iCloud app working would be greatly appreciated!
>
> Those two keys you add when you open the persistent store, NSPersistentStoreUbiquitousContentNameKey and NSPersistentStoreUbiquitousContentURLKey are the ones which tell Core Data you're using the log file based core data (only available with SQLite). So with that store, the actual SQLite database isn't in the cloud, it's kept local, but a log file directory is created which is in the cloud and deltas are synched up there. The idea is that each client just brings down the log files and updates the database at a record level. Those keys only mean anything with the SQLite store, which may be why you're having issues with migration. I don't use any of those keys, I just have my store as a local file which is synched wholesale.
>
> For your original mail, you want to migrate to SQLLite and then also migrate to iCloud. I don't know if you can do that easily in one step. If I were looking at this I would probably think of creating a new SQLLite store for the migration, empty, opening the old local XML store and then migrating the objects over with code. Whether you choose to make the new SQLite store local and then migrate it up to iCloud or make it in the cloud and then update it is a question I don't have a good answer to, I'm still a little confused by how the initial log files get magically created when you migrate a document to iCloud, I'm definitely missing a piece of information somewhere.
I think I've achieved this migration step with code like this:
NSURL *oldURL = [NSURL fileURLWithPath:[applicationSupportDirectory stringByAppendingPathComponent:oldStoreName]];
NSURL *newURL = [NSURL fileURLWithPath:[applicationSupportDirectory stringByAppendingPathComponent:storeName]];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if ([fileManager fileExistsAtPath:[oldURL path]]) {
NSError *error = nil;
NSPersistentStore *store = [persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType
configuration:nil
URL:oldURL
options:options
error:&error];
if (!store){
[[NSApplication sharedApplication] presentError:error];
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
return nil;
}
// now add iCloud options
[options setObject:storeName forKey:NSPersistentStoreUbiquitousContentNameKey];
NSURL *contentURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:containerID];
[options setObject:contentURL forKey:NSPersistentStoreUbiquitousContentURLKey];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSValidateXMLStoreOption];
// migrate it
store = [persistentStoreCoordinator migratePersistentStore:store toURL:newURL options:options withType:NSSQLiteStoreType error:&error];
if (error) {
[NSApp presentError:error];
return nil;
}
// archive old store
[fileManager moveItemAtURL:oldURL toURL:[oldURL URLByAppendingPathExtension:@"xml"] error:&error];
if (error) {
[NSApp presentError:error];
}
} else {
// add iCloud
[options setObject:storeName forKey:NSPersistentStoreUbiquitousContentNameKey];
NSURL *contentURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:containerID];
[options setObject:contentURL forKey:NSPersistentStoreUbiquitousContentURLKey];
// make a new persistent store coordinator
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:newURL
options:options
error:&error]){
[[NSApplication sharedApplication] presentError:error];
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
return nil;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: email@hidden
WWW: http://www.aei.mpg.de/~hewitson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden