在过去的四个小时中,我一直在寻找解决方案,而我却绝望地发帖。在向一个数据模型添加属性之前,我的应用在模拟器,iPhone和ipad上均能完美运行。

我的iPhone应用程序使用Core Data和iCloud。在AppDelegate中,我通过合并两个模型来创建managedObjectModel。在尝试保存ManagedObjectContext之前,一切似乎都很好。那是它崩溃的原因:



这不会在模拟器上发生。

我努力了:

  • 项目->清洁构建文件夹
  • 项目->清洁
  • 从我的应用中删除
    设备
  • 从我的iCloud备份中删除iCloud数据
  • 重新启动计算机
  • 将“.momd”更改为“.mom”,然后再次返回(在另一个问题中进行了了解)

  • 谢谢您的帮助。

    编辑添加代码:
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }
    
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    NSPersistentStoreCoordinator *psc = __persistentStoreCoordinator;
    
    // TODO: Set up iCloud in another thread:
    //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSString *dataFileName = [NSString stringWithFormat:@"%@.sqlite", APP_TITLE_NO_SPACES];
    
    NSString *iCloudDataDirectoryName = @"Data.nosync";
    NSString *iCloudLogsDirectoryName = @"Logs";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
    NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
    
    if (iCloud) {
        NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];
        if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
            NSError *fileSystemError;
            [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
                   withIntermediateDirectories:YES
                                    attributes:nil
                                         error:&fileSystemError];
            if(fileSystemError != nil) {
                NSLog(@"Error creating database directory %@", fileSystemError);
            }
        }
    
        NSString *iCloudData = [[[iCloud path]
                                 stringByAppendingPathComponent:iCloudDataDirectoryName]
                                stringByAppendingPathComponent:dataFileName];
    
        NSLog(@"iCloudData = %@", iCloudData);
    
        NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                 CLOUD_CONTAINER_IDENTIFIER, NSPersistentStoreUbiquitousContentNameKey,
                                 iCloudLogsPath, NSPersistentStoreUbiquitousContentURLKey, nil];
    
        [psc lock];
        [psc addPersistentStoreWithType:NSSQLiteStoreType
                          configuration:nil
                                    URL:[NSURL fileURLWithPath:iCloudData]
                                options:options
                                  error:nil];
        [psc unlock];
    } else {
        NSLog(@"iCloud is NOT working - using a local store");
        NSMutableDictionary *options = [NSMutableDictionary dictionary];
        [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
        [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
    
        [psc lock];
    
        [psc addPersistentStoreWithType:NSSQLiteStoreType
                          configuration:nil
                                    URL:localStore
                                options:options
                                  error:nil];
        [psc unlock];
    }
    __persistentStoreCoordinator = psc;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ICLOUD_SOMETHING_CHANGED object:nil];
    
    return __persistentStoreCoordinator;
    

    }

    最佳答案

    通常,删除程序数据后应该可以。但是,错误表明您没有不一致,而根本没有存储。假设:如果发生故障,请勿重新启动它。也许以下代码可能会有用:

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    
    {
        if (__persistentStoreCoordinator != nil) {
            return __persistentStoreCoordinator;
        }
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myApp.sqlite"];
    
        NSError *error = nil;
        __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
        if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
            // Error, erase data
            [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
            NSLog(@"store cleaned");
            __persistentStoreCoordinator = nil;
            return [self persistentStoreCoordinator];
        }
        return __persistentStoreCoordinator;
    }
    

    10-08 06:37
    查看更多