从iCloud迁移到本地商店崩溃应用程序,迁移时"Object cannot be nil" - 使用核心数据



我有一个仅限iOS 7的应用程序,它使用核心数据进行存储,我带来了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,但后来选择不使用,他们的数据应该在本地迁移。

这个(迁移持久存储崩溃)堆栈溢出问题似乎非常适合,但首先,答案没有被接受,提问者也没有确认代码有效,而且代码对我不起作用;它只是简单地删除了数据。

如能就此提供任何指导,我们将不胜感激。

通过密切关注链接:迁移持久存储崩溃,我能够使用以下代码实现这一点:

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

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

相关内容

最新更新