我的persistentStoreCoordinator 中有以下代码。没有 iCloud 部分,它工作正常。对于 iCloud,它在 addPersistentStoreWithType 方法上停止。没有错误,它只是停止并且不会继续。
有什么想法吗?
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [NSURL fileURLWithPath:STORE_PATH];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
NSLog(@"icloud: %@", iCloud);
if (iCloud) {
NSString *iCloudLogsDirectoryName = @"Logs";
NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];
//Create logs directory, in case it doesn't exist
if([fileManager fileExistsAtPath:[iCloudLogsPath path]] == NO) {
NSLog(@"logs directory doesn't exist");
NSError *fileSystemError;
[fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]
withIntermediateDirectories:YES
attributes:nil
error:&fileSystemError];
if(fileSystemError != nil) {
NSLog(@"Error creating logs directory %@", fileSystemError);
}
}
NSString *iCloudEnabledAppID = @"app id removed from stackoverflow";
[options setObject:iCloudEnabledAppID forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:iCloudLogsPath forKey:NSPersistentStoreUbiquitousContentURLKey];
NSLog(@"logs path: %@", iCloudLogsPath);
}
[_persistentStoreCoordinator lock];
NSError *error;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
else {
NSLog(@"done adding persistent store");
}
[_persistentStoreCoordinator unlock];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged" object:self userInfo:nil];
[self.delegate contextWasSetupInManager:self];
});
});
return _persistentStoreCoordinator;
}