核心数据iCloud迁移错误:代码= 513"您无权将文件“store”保存在文件夹中

时间:2017-02-03 19:57:13

标签: ios objective-c core-data core-data-migration

尝试将我的core data迁移到icloud时我得到了这个:

  

Code = 513"您无权将文件“store”保存在   夹

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }


    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"app.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }


    NSFileManager *fm = [NSFileManager defaultManager];
    NSURL *url = [fm URLForUbiquityContainerIdentifier:nil];

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

 if (url)
    {

        //Cloud store
        NSDictionary *options = @{
                                  NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES,
                                  NSPersistentStoreRebuildFromUbiquitousContentOption:@YES,
                                  NSReadOnlyDocumentAttribute:@NO
                                  };

        // Local
        NSPersistentStore *localStore = [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];

        // Cloud
        NSPersistentStore *migratedStore = [__persistentStoreCoordinator migratePersistentStore:localStore toURL:storeURL options:[self iCloudStoreOptions] withType:NSSQLiteStoreType error:&error];
        NSLog(@"\n\nThe migrationStore is:  %@\n\n", migratedStore);

        // Migrate
        [__persistentStoreCoordinator migratePersistentStore:migratedStore toURL:[self iCloudURL] options:[self iCloudStoreOptions] withType:NSSQLiteStoreType error:&error];

    }
    else
    {
        //Local store
        NSLog(@"iCloud is not enabled or not purchased");
        NSDictionary *options = @{
                                  NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES
                                  };

        if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self storeURL] options:options error:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();

        }
    }
    dispatch_async(dispatch_get_main_queue(), ^{

        [[NSNotificationCenter defaultCenter] postNotificationName:@"Reload" object:self userInfo:nil];
    });

iCloud商店选项:

-(NSDictionary *)iCloudStoreOptions{
    NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:NO] forKey:NSReadOnlyDocumentAttribute];
    [options setObject:@"myStore" forKey:NSPersistentStoreUbiquitousContentNameKey];
    [options setObject:@"DELETE" forKey:@"journal_mode"];
    return options;
}

0 个答案:

没有答案