我的 persistenceStoreCoordinator 中有以下代码。没有 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;
}
最佳答案
这在 iCloud 中很常见。 Apple 建议您将其放入后台队列,因为该调用可能会阻塞。它会阻塞,因为这是 Core Data 实际上与 iCloud 服务器建立连接并开始下载任何可用新数据的地方。调用在结束之前不会继续。如果有大量数据需要下载,您可能需要等待一段时间。
有时会因为没有明显原因而需要一段时间。就是这样的片状。
最终,您所看到的与 iCloud 目前的情况差不多。提交一两个错误并希望事情有所改善。
关于iphone - 应用程序在 iCloud 上的 addPersistentStoreWithType 上停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14480539/