2

我正在为基于 ios 5 的 Core Data 应用程序添加 iCloud 支持。我可以在第一台设备上很好地安装该应用程序,并将数据上传到 iCloud。当我尝试在第二台设备上安装应用程序进行测试时,我收到此错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFDictionary setObject:forKey:]:尝试插入零键”

这是我的相关 iCloud 代码:

// ** 注意:如果您修改此代码以供自己使用,则必须更改此变量: NSString *iCloudEnabledAppID = @"com.strikelabs.iXercise";

    // ** Note: if you adapt this code for your own use, you should change this variable:        
    NSString *dataFileName = @"Xercise.sqlite";

    // ** Note: For basic usage you shouldn't need to change anything else

    NSString *iCloudDataDirectoryName = @"Data";
    NSString *iCloudLogsDirectoryName = @"Logs";
    NSFileManager *fileManager = [NSFileManager defaultManager];        
    NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];

    NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];

    if (iCloud) {

        NSLog(@"iCloud is working");

        NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];

        NSLog(@"iCloudEnabledAppID = %@",iCloudEnabledAppID);
        NSLog(@"dataFileName = %@", dataFileName); 
        NSLog(@"iCloudDataDirectoryName = %@", iCloudDataDirectoryName);
        NSLog(@"iCloudLogsDirectoryName = %@", iCloudLogsDirectoryName);  
        NSLog(@"iCloud = %@", iCloud);
        NSLog(@"iCloudLogsPath = %@", iCloudLogsPath);

        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);

        NSMutableDictionary *options = [NSMutableDictionary dictionary];
        [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
        [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
        [options setObject:iCloudEnabledAppID            forKey:NSPersistentStoreUbiquitousContentNameKey];
        [options setObject:iCloudLogsPath                forKey:NSPersistentStoreUbiquitousContentURLKey];

        [psc lock];

        [psc addPersistentStoreWithType:NSSQLiteStoreType 
                          configuration:nil 
                                    URL:[NSURL fileURLWithPath:iCloudData] 
                                options:options 
                                  error:nil];

        [psc unlock];

据我所知,该错误发生在 addPersistentStoreWithType 期间。我在 iPhone 4S 和 iPad 2 上进行测试。无论我安装应用程序的顺序如何,第二台设备都会出现错误。

任何帮助,将不胜感激!

每当我的程序有 iCloud 实体并且该程序安装在设备上时,就会出现错误。

编辑:看起来这是固定的。您必须将 .nosync 作为 Core Data 的本地存储位置的后缀。

4

0 回答 0