简体   繁体   English

在迁移时使用“对象不能为零”从iCloud迁移到本地崩溃的应用程序-使用核心数据

[英]Migrating from iCloud to Local store crashing app with “Object cannot be nil” at the migration - with the use of Core Data

I have an iOS 7 only app which uses Core Data for storage and I am bringing iCloud, with a switch to the app. 我有一个仅使用iOS 7的应用程序,该应用程序使用Core Data进行存储,而我带来了iCloud,并切换到该应用程序。

Every aspect of the iCloud integration is working, except the migration from the iCloud Store to the Local store if the user turns off iCloud from within the app. iCloud集成的各个方面都可以正常工作,但如果用户从应用程序中关闭iCloud,则从iCloud Store迁移到本地存储的操作除外。

Through the use of an Exception Breakpoint , the app is crashing with: 通过使用Exception Breakpoint ,应用程序崩溃并出现:

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

This is crashing at the " migratePersistentStore " code. 这在“ migratePersistentStore ”代码处崩溃。

This is the code that performs that: 这是执行该代码的代码:

- (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];
}

Issue 问题

The app will crash, with the error above, at the migratePersistentStore line above. 该应用程序将在上面的migratePersistentStore行崩溃,并出现以上错误。 I cannot figure out what to do to get this working. 我无法弄清楚该如何做。

The commented out code for the currentStore shows that I've also tried checking for the lastObject, instead of the firstObject, and in both cases, I'm getting the same result. currentStore的注释掉的代码表明,我也尝试过检查lastObject而不是firstObject,在两种情况下,我都得到相同的结果。

I don't get any crashes from the local to iCloud because I want to make sure, if the user is using iCloud but then chooses not to, their data should be migrated locally. 我没有从本地崩溃到iCloud,因为我想确保,如果用户使用的是iCloud,然后选择不使用,则他们的数据应在本地迁移。

This ( Migrate Persistant Store Crash ) Stack Overflow question seems like it would be a perfect fit, but firstly the answer isn't accepted and there's no confirmation that the code works from the person asking the question and also, that code didn't work for me; 这个( 迁移持久性存储崩溃 )堆栈溢出问题似乎很合适,但是首先答案没有被接受,也没有确认代码是否可以从问这个问题的人那里获得,并且,该代码也不起作用为了我; it simply removed the data. 它只是删除了数据。

Any guidance on this would really be appreciated. 对此的任何指导将不胜感激。

With paying close attention to the link : Migrate Persistant Store Crash , I was able to get this working with the following code: 通过密切注意链接: 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);

Hopefully this will help someone with the same issue 希望这可以帮助遇到同样问题的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM