将iCloud迁移到本地持久性存储

时间:2015-02-15 17:20:35

标签: ios core-data icloud core-data-migration

我的想法很少我怎么可能将iCloud商店迁移到本地商店。 从一边我得到了崩溃信息:

The specified persistent store was not found

在另一边

NSUnderlyingException = "Can't add the same store twice"

重点是没有明确的教程如何迁移商店(或者至少我无法找到)。到目前为止,我发现的所有信息都是遍布网络的信息,并不是很有意义。

这是我的代码:

- (NSPersistentStoreCoordinator *) cloudPersistentStoreCoordinator {
    self.storeURL = [[MMNUtilities localDocumentsURL] URLByAppendingPathComponent:@"myapp.sqlite"];
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
    NSDictionary *storeOptions = @{NSPersistentStoreUbiquitousContentNameKey: @"myapp"};

    NSError *error = nil;
    self.persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                     configuration:nil
                                                                               URL:self.storeURL
                                                                           options:storeOptions
                                                                             error:&error];

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

    return persistentStoreCoordinator;
}

- (BOOL) didMigrateiCloudStoreToLocalStore {
    NSError *error = nil;

    self.persistentStoreCoordinator = [self cloudPersistentStoreCoordinator];
    NSDictionary *storeOptions      = @{NSPersistentStoreUbiquitousContentNameKey: @"myapp",
                                        NSPersistentStoreRemoveUbiquitousMetadataOption : @YES};

    NSPersistentStore *localStore   = [self.persistentStoreCoordinator migratePersistentStore:self.persistentStore  <<< Exception happens here 
                                                                                        toURL:self.storeURL
                                                                                      options:storeOptions
                                                                                     withType:NSSQLiteStoreType
                                                                                        error:&error];

    if (error) {
        NSLog(@"Error: %@", error.description);
        [[NSNotificationCenter defaultCenter] postNotificationName:HnHCoreDataPersistentStoreMigrationFailed object:self];
        return NO;
    }

    return [self reloadStore:localStore];
}

我在控制台中收到以下消息:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:PF_DEFAULT_CONFIGURATION_NAME URL:file:///var/mobile/Containers/Data/Application/EFC750F6-D10E-4B8A-9F83-C77621218DB7/Documents/myapp.sqlite options:{ NSPersistentStoreRemoveUbiquitousMetadataOption = 1; NSPersistentStoreUbiquitousContentNameKey = myapp; "_NSNotifyObserversOfStoreChange" = 0; } ... returned error Error Domain=NSCocoaErrorDomain Code=134080 "The operation couldn’t be completed. (Cocoa error 134080.)" UserInfo=0x15deee80 {NSUnderlyingException=Can't add the same store twice} with userInfo dictionary { NSUnderlyingException = "Can't add the same store twice"; }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要创建一个名为的新本地商店