将uimangeddocument从icloud迁移到本地(并返回)

时间:2013-08-02 11:52:45

标签: ios icloud uimanageddocument

我可以为icloud / local创建UIManagedDocument:

- (UIManagedDocument *)initWithFileURL:(NSURL *)url isCloudEnabled:(BOOL)isCloudEnabled
{
self = [super initWithFileURL:url];
if (self)
{
    NSDictionary *options;

    if (isCloudEnabled)
    {
        options = [self cloudOptions];
    }
    else
    {
        options = [self localOptions];
    }

    self.persistentStoreOptions = options;
}

return self;
}

- (NSDictionary *)cloudOptions
{
return [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
        self.fileURL.lastPathComponent.stringByDeletingPathExtension, NSPersistentStoreUbiquitousContentNameKey,
        self.containerURL, NSPersistentStoreUbiquitousContentURLKey,
        nil];
}

- (NSDictionary *)localOptions
{
return [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
        nil];
}

- (NSURL*)containerURL
{
static NSURL* url = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
    url = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
});

return url;
}

如果我使用icloud,那么我将文件移动到icloud:

- (NSURL *)moveToiCloud:(NSURL *)localURL
{
NSURL* destinationURL = [[[MSCloudManager sharedManager] documentsDirectoryURL] URLByAppendingPathComponent:[localURL lastPathComponent]];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {

    NSFileManager* fileManager = [[NSFileManager alloc] init];
    NSError *error;
    if (![fileManager setUbiquitous:YES itemAtURL:localURL destinationURL:destinationURL error:&error])
    {
        NSLog(@"Can not save file into iCloud: %@",error.description);
    }
});
return destinationURL;
}

使用NSPersistentStoreDidImportUbiquitousContentChangesNotification监控变更和迁移:[context mergeChangesFromContextDidSaveNotification:notification]; 我需要将iCloud中的UIManagedDocument移动到本地并返回。 我使用下一个代码:

- (NSURL *)moveFromiCloud:(NSURL *)cloudURL completionHandler:(void (^)(NSURL *docURL))completionHandler
{
NSURL *destinationURL = [self.localDocumentsURL URLByAppendingPathComponent:cloudURL.lastPathComponent];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {

    NSFileManager* fileManager = [[NSFileManager alloc] init];
    NSError *error;
    if (![fileManager setUbiquitous:NO itemAtURL:cloudURL destinationURL:destinationURL error:&error])
    {
        NSLog(@"Can not save file into iCloud: %@",error.description);
    }
    else
    {
        dispatch_sync(dispatch_get_main_queue(), ^(void) {
            completionHandler(destinationURL);
        });
    }
});
return destinationURL;
}

接下来我尝试打开文件,如果我使用

- (NSDictionary *)localOptions
{
return [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
        nil];
}

对于managedDoc.persistentStoreOptions然后我无法打开文件。 如果我使用

- (NSDictionary *)cloudOptions
{
return [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
        self.fileURL.lastPathComponent.stringByDeletingPathExtension, NSPersistentStoreUbiquitousContentNameKey,
        self.containerURL, NSPersistentStoreUbiquitousContentURLKey,
        nil];
}

对于managedDoc.persistentStoreOptions然后我得到空基础。 这个想法 - 让用户可以选择存储特定文件的位置(本地或iCloud)。

0 个答案:

没有答案