Using iCloud enabled Core Data NSArray-taking method

倖福魔咒の 提交于 2019-12-24 02:08:12

问题


I've been setting up and testing Core Data in my app and everything works great locally; however, as soon as I enable iCloud via:

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

EDIT3: Here's the code that initializes my context:

- (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:


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. NSArray is ordered and duplicate-inclusive, NSSet is unordered and duplicate-exclusive. 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.



来源:https://stackoverflow.com/questions/28334710/using-icloud-enabled-core-data-nsarray-taking-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!