App Crash if iCloud is on (linked to sync with coreData)

我只是一个虾纸丫 提交于 2019-12-12 09:19:58

问题


This is the code i used in the init for storage class and it worked fine until recently.. when i tried testing it, it crashes (marked below) at addPersistentStoreWithType ... i noticed this after switching to the iPhone 5 and was trying to run the app... but if i turn off icloud on my phone or test it on the simulator there are no problems...

-(id)init
{
    self = [super init];
    if(self)
    {
        NSLog(@"%s", __FUNCTION__);
        favColors = [[NSMutableArray alloc] init];
        model = [NSManagedObjectModel mergedModelFromBundles:nil];
        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        NSString *path = [self itemArchivePath];
        NSURL *storeURL = [NSURL fileURLWithPath:path];
        NSError *error = nil;
        NSMutableDictionary *options = [NSMutableDictionary dictionary];

        NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
        if (ubiq) {
            NSLog(@"iCloud access at %@", ubiq);
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(contentChange:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:nil];
            NSFileManager *fm = [NSFileManager defaultManager];
            NSURL *ubcontainer = [fm URLForUbiquityContainerIdentifier:nil];
            [options setObject:@"color" forKey:NSPersistentStoreUbiquitousContentNameKey];
            [options setObject:ubcontainer forKey:NSPersistentStoreUbiquitousContentURLKey];

        } else {
            NSLog(@"No iCloud access");
        }
   // ************ Crash here **************
        if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:(ubiq?options:nil) error:&error]) {
            [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
        }
        context = [[NSManagedObjectContext alloc] init];
        [context setPersistentStoreCoordinator:psc];
        [context setUndoManager:nil];

        [self loadAllItems];
    }
    return self;
}

- (NSString *)itemArchivePath
{
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"store.data"];
}

Also if i run the app once and turn off icloud after the crash and run the app again.. the data is retrieved from icloud... and shows on app.

The out put log shows this.

iCloud access at file://localhost/private/var/mobile/Library/Mobile%20Documents/xxx -PFUbiquitySetupAssistant performPreStoreSetupWithError:: CoreData: Ubiquity: The baseline file exists, but could not be read


回答1:


That's an internal iCloud error. It's not your fault, and there's nothing you can do in code that would fix or prevent it. It will happen randomly, with no predictable pattern for when or why. Deleting data for the app is, unfortunately, the only real solution. Doing that only deletes iCloud's copy of the data, not data stored in your app's Documents directory or other non-iCloud locations, so it's not surprising that the old data would still be present.



来源:https://stackoverflow.com/questions/15319221/app-crash-if-icloud-is-on-linked-to-sync-with-coredata

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