我可以为 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]; 我需要将 UIManagedDocument 从 iCloud 移动到本地并返回。我使用下一个代码:
- (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)。