使用iCloud启用Core Data NSArray-taking方法

时间:2015-02-05 01:28:15

标签: ios objective-c xcode core-data icloud

我一直在我的应用程序中设置和测试核心数据,一切都在本地运行良好;但是,只要我通过以下方式启用iCloud:

NSDictionary *options;
    if ([self iCloudEnabled]) {
        options = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"},
                    NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore"};
        [self subscribeToNotifications];
    } else
        options = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"}};

运行我的应用程序,我在控制台中收到以下错误信息:

*** ERROR: this process has called an NSArray-taking method, such as initWithArray:, and passed in an NSSet object.  This is being worked-around for now, but will soon cause you grief.

当打印完错误后,一切似乎都能正常工作。

我能找到的唯一其他相关问题是herehere,而且这些问题都没有帮助。

我甚至找不到任何说明错误含义的地方。

感谢任何帮助。

编辑:从我发现在我的managedObjectContext初始化之后和“使用本地存储:0”打印到控制台之前发生的事情。问题是,我不知道那时候正在执行什么(因为我没有打电话);它似乎是在后台线程中。

EDIT2:我还应该提一下,这只会在第一次启动应用程序时发生(在iCloud“一次性”设置上)。

EDIT3:这是初始化我的上下文的代码:

- (NSURL *)coreDataLocalURL {
    // The directory the application uses to store the Core Data store file.
    NSURL *result = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

    result = [result URLByAppendingPathComponent:@"TheAnglersLog"];

    NSError *e;
    if (![[NSFileManager defaultManager] fileExistsAtPath:result.path])
        // create TheAnglersLog directory if it doesn't exist
        [[NSFileManager defaultManager] createDirectoryAtPath:result.path
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:&e];

    return result;
}

- (NSManagedObjectModel *)managedObjectModel {
    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
    if (_managedObjectModel != nil)
        return _managedObjectModel;

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TheAnglersLog" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    // Create the coordinator and store

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self coreDataLocalURL] URLByAppendingPathComponent:@"TheAnglersLog.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";

    NSDictionary *options;
    if ([self iCloudEnabled]) {
        options = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"},
                    NSPersistentStoreUbiquitousContentNameKey : @"TheAnglersLogCloudStore"};
        [self subscribeToNotifications];
    } else
        options = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"}};

    NSLog(@"Is iCloud enabled? %@", [self iCloudEnabled] ? @"YES" : @"NO");

    NSPersistentStore *store;

    if (!(store = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])) {
        // Report any errors we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];

        NSLog(@"Error in persistentStoreCoordinator: %@, %@", error, [error userInfo]);
    }

    NSLog(@"Core Data URL: %@", [store URL]);

    return _persistentStoreCoordinator;
}

- (NSManagedObjectContext *)managedObjectContext {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

1 个答案:

答案 0 :(得分:0)

它正好告诉你问题:某个地方(不在你发布的代码中),你将NSSet传递给期望NSArray的东西。 NSArray是有序且包含重复的,NSSet是无序的,并且是重复排除的。它将它转换为适合你的一套,所以它不会崩溃,但是抱怨它将来可能不会继续这样做。