这是我在 init 中用于存储类的代码,直到最近它都运行良好。当我尝试测试它时,它在 addPersistentStoreWithType 处崩溃(标记如下)......我在切换到 iPhone 5 后注意到了这一点并试图运行应用程序...但是如果我在手机上关闭 icloud 或在模拟器上对其进行测试,则没有问题...
-(id)init
{
self = [super init];
if(self)
{
NSLog(@"%s", __FUNCTION__);
favColors = [[NSMutableArray alloc] init];
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSString *path = [self itemArchivePath];
NSURL *storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
NSMutableDictionary *options = [NSMutableDictionary dictionary];
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSLog(@"iCloud access at %@", ubiq);
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(contentChange:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:nil];
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *ubcontainer = [fm URLForUbiquityContainerIdentifier:nil];
[options setObject:@"color" forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:ubcontainer forKey:NSPersistentStoreUbiquitousContentURLKey];
} else {
NSLog(@"No iCloud access");
}
// ************ Crash here **************
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:(ubiq?options:nil) error:&error]) {
[NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
}
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
[context setUndoManager:nil];
[self loadAllItems];
}
return self;
}
- (NSString *)itemArchivePath
{
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"store.data"];
}
此外,如果我运行该应用程序一次并在崩溃后关闭 icloud 并再次运行该应用程序.. 数据将从 icloud 中检索...并显示在应用程序上。
输出日志显示了这一点。
iCloud 访问文件://localhost/private/var/mobile/Library/Mobile%20Documents/xxx -PFUbiquitySetupAssistant performPreStoreSetupWithError:: CoreData: Ubiquity: 基线文件存在,但无法读取