What is the code to move UIManagedDocument which has some data from local to iCloud directory? I understand i need to use migratePersistentStore feature, but when i don't understand how should i do that with a managed document, and there is no examples of how to do this. Im struggling for 2 weeks now. There are a lot of these questions on stack overflow, but none of them are getting an answer? Is is this hard? Just one method to move existing document to iCloud?
-(void)transferFileFromLocalDirectory:(NSURL*)localURL ToiCloudDirectory:(NSURL*)iCloudURL{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Successfully set document ubiquitous");
UIManagedDocument* doc=[[UIManagedDocument alloc]initWithFileURL:iCloudURL];
doc.persistentStoreOptions=[self getPersistentOptionsFromDocumentMetadataAtURL:[iCloudURL URLByAppendingPathComponent:@"DocumentMetadata.plist"]];
[doc saveToURL:doc.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
UIManagedDocument* localDoc=[[UIManagedDocument alloc]initWithFileURL:localURL];
NSPersistentStoreCoordinator* psc=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:localDoc.managedObjectModel];
NSPersistentStore* store=[[psc persistentStores]lastObject];
if(!store){
store=[[NSPersistentStore alloc]initWithPersistentStoreCoordinator:psc configurationName:nil URL:[[localURL URLByAppendingPathComponent:@"StoreContent"]URLByAppendingPathComponent:@"persistentStore"] options:nil];
}
NSLog(@"store : %@",store);
NSError* error2=nil;
[psc migratePersistentStore:store toURL:[[localURL URLByAppendingPathComponent:@"StoreContent"]URLByAppendingPathComponent:@"persistentStore"] options:nil withType:NSSQLiteStoreType error:&error2];
if(error){
NSLog(@"error migrating persistent store:\n%@",error);
}
}];
});
}
i get this output
2013-12-07 20:15:45.284 iGymPRO[1111:3707] store : <NSPersistentStore: 0x15eb7a20> (URL: file:///var/mobile/Applications/610A9443-002A-42EA-8A66-E4D43F609672/Documents/Data%20Document/StoreContent/persistentStore)
2013-12-07 20:15:45.290 iGymPRO[1111:3707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't find store type for store <NSPersistentStore: 0x15eb7a20> (URL: file:///var/mobile/Applications/610A9443-002A-42EA-8A66-E4D43F609672/Documents/Data%20Document/StoreContent/persistentStore) (class == NSPersistentStore) in {
Binary = "<8c3b9f3a>";
InMemory = "<54419f3a>";
SQLite = "<dc459f3a>";
}.'
Check out this link explaining UIManagedDocument and iCloud Integration and providing details of the classes and methods involved in doing this
Also take a look at this video showing how the app works when creating UIManagedDocuments and moving them to and from iCloud to confirm its what you are trying to achieve.