简体   繁体   English

使用启用iCloud的Core Data NSArray采取方法

[英]Using iCloud enabled Core Data NSArray-taking method

I've been setting up and testing Core Data in my app and everything works great locally; 我一直在我的应用程序中设置和测试Core Data,并且在本地一切正常。 however, as soon as I enable iCloud via: 但是,只要我通过以下方式启用iCloud:

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

And run my app, I get the following error spammed in the console: 并运行我的应用程序,我在控制台中收到以下错误消息:

*** 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.

When that error is done printing, everything seems to work normally. 打印完该错误后,一切似乎都可以正常工作。

The only other related questions I could find were here and here , and neither of them were helpful. 我能找到的唯一其他相关问题在这里这里 ,都没有帮助。

I can't even find anywhere that says what that error means. 我什至找不到任何地方表明该错误的含义。

Any help is appreciated. 任何帮助表示赞赏。

EDIT: From what I've found it's happening after my managedObjectContext is initialized and before the "Using local storage: 0" is printed to the console. 编辑:从我发现它发生在我的ManagedObjectContext初始化之后和“使用本地存储:0”打印到控制台之前发生。 Problem is, I have no idea what's being executed in that time (because I'm not calling anything); 问题是,我不知道那段时间正在执行什么(因为我什么也没叫)。 it seems to be in a background thread. 它似乎在后台线程中。

EDIT2: I should also mention that this only happens the first time the app is launched (on the iCloud "one-time" setup). EDIT2:我还应该提到,这仅在应用程序首次启动时发生(在iCloud“一次性”设置上)。

EDIT3: Here's the code that initializes my context: 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;
}

It's telling you precisely the problem: Somewhere (not in the code you posted), you're passing an NSSet to something which expects an NSArray . 它正好告诉您问题所在:在某个地方(不在您发布的代码中),您正在将NSSet传递给需要NSArray东西。 NSArray is ordered and duplicate-inclusive, NSSet is unordered and duplicate-exclusive. NSArray是有序的且包含重复项, NSSet是无序的且包含重复项。 It converted it to a set for you so it wouldn't crash, but is complaining it may not continue to do so in the future. 它为您将其转换为一个集,因此它不会崩溃,但抱怨它将来可能不会继续这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM