1

我有一个仅限 iOS 7 的应用程序,它使用 Core Data 进行存储,我带来了 iCloud,并切换到该应用程序。

iCloud 集成的各个方面都在工作,除了如果用户从应用程序内关闭 iCloud,则从 iCloud 商店迁移到本地商店。

通过使用Exception Breakpoint,应用程序崩溃:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil'

这在“ migratePersistentStore”代码处崩溃。

这是执行该操作的代码:

- (void)migrateiCloudStoreToTheLocalStoreAfterUserTurnedOffiCloudInSettings
{    
    // So we can see what's going on, we'll write out the current store URL before the migration
    NSURL *storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
    NSLog(@"Current Store URL (before iCloud to Local migration): %@", [storeURL description]);

    //    NSPersistentStore *currentStore =     self.persistentStoreCoordinator.persistentStores.lastObject;

    NSPersistentStore *currentStore = [[self.persistentStoreCoordinator persistentStores] firstObject];
    // We'll create a new URL
    NSURL *localURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Envylope.sqlite"];

    NSDictionary *localStoreOptions = nil;
    localStoreOptions = @{ NSPersistentStoreRemoveUbiquitousMetadataOption : @YES,
                           NSMigratePersistentStoresAutomaticallyOption : @YES,
                           NSInferMappingModelAutomaticallyOption : @YES};

    NSError *error = nil;
    [self.persistentStoreCoordinator migratePersistentStore:currentStore
                                                      toURL:localURL
                                                    options:localStoreOptions
                                                   withType:NSSQLiteStoreType error:&error];

    NSLog(@"Current Store URL (after iCloud to Local migration): %@", [localURL description]);

    // We'll write out a NSError here to see if there were any errors during the migration
    NSLog(@"Error from iCloud to local migration %@", error);

    // We'll just do a quick check to make sure are no errors with this procedure.
    NSLog(@"Erros's after all of that %@", error);

    NSLog(@"Current Store URL (after everything): %@", [localURL description]);

    [self removeCloudObservers];
}

问题

该应用程序将在上面的行中崩溃,并出现上述错误migratePersistentStore。我不知道该怎么做才能使它正常工作。

currentStore 的注释掉代码表明我也尝试检查 lastObject,而不是 firstObject,并且在这两种情况下,我都得到了相同的结果。

我没有从本地到 iCloud 发生任何崩溃,因为我想确保,如果用户正在使用 iCloud 但随后选择不使用,他们的数据应该在本地迁移。

这个(Migrate Persistant Store Crash)堆栈溢出问题似乎是一个完美的选择,但首先答案不被接受,并且没有确认代码可以从提出问题的人那里得到,而且该代码也不起作用为了我; 它只是删除了数据。

对此的任何指导将不胜感激。

4

1 回答 1

2

密切关注链接:Migrate Persistant Store Crash,我能够使用以下代码使其工作:

NSPersistentStoreCoordinator * persistentStoreCoordinator = self.persistentStoreCoordinator;

NSPersistentStore * persistentStore = [[persistentStoreCoordinator persistentStores] firstObject];

if([[NSFileManager defaultManager]fileExistsAtPath:localURL.path])
{
    NSLog(@"File exists");
    [[NSFileManager defaultManager] removeItemAtPath:localURL.path error:&error];
    NSLog(@"Removing error = %@", error); 

}

[[NSFileManager defaultManager] copyItemAtPath:persistentStore.URL.path toPath:localURL.path error:&error];

NSLog(@"The copying error = %@", error);

NSPersistentStoreCoordinator * newPersistentStoreCoordinator;

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

[newPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType

                                            configuration:nil

                                                      URL:localURL

                                                  options:localStoreOptions

                                                    error:&error];

NSLog(@"The adding error = %@", error);

希望这会帮助有同样问题的人

于 2014-08-28T08:11:51.190 回答