如何将UIManagedDocument移动到iCloud

时间:2013-12-07 17:15:04

标签: core-data icloud uimanageddocument nspersistentstore

移动UIManagedDocument的代码是什么,其中包含从本地到iCloud目录的一些数据? 我理解我需要使用migratePersistentStore功能,但是当我不明白我应该如何使用托管文档时,并且没有如何执行此操作的示例。 我现在挣扎了两个星期。堆栈溢出有很多这些问题,但没有一个得到答案?难道这难吗?只需一种方法将现有文档移至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);
                }
            }];
    });
}

我得到了这个输出

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>";
}.'