Migration de NSPersistentStore du sandbox de l'application vers le conteneur de groupe partagé

J'essaie de déplacer mon NSPersistentStore du sandbox de mon application vers un conteneur de groupe partagé afin que je puisse accéder à CoreData depuis mon extension WatchKit. J'utilise actuellement Core Data avec iCloud et je souhaite déplacer datatables des users vers le conteneur de groupe partagé. Actuellement, je crée le NSPersistentStoreCoordinator comme suit:

if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } NSURL *url = [self storeURL]; // app's Documents directory NSError *error = nil; __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:[self iCloudPersistentStoreOptions] error:&error]) { NSLog(@"Error adding persistent store: %@", [error localizedDescription]); } // rest of setup return __persistentStoreCoordinator; 

J'ai déjà configuré le conteneur de groupe partagé dans ma cible d'application et ma cible d'extension WatchKit et je peux get le NSURL pour le nouvel location de magasin en utilisant - (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSSsortingng *)groupIdentifier .

Comment puis-je vérifier si j'ai besoin de migrer ou si j'ai déjà migré afin de ne pas tenter de migrer plusieurs fois? Au départ, je pensais à quelque chose comme ça, mais cela ne fonctionne pas, car l'ancienne URL de magasin n'existe pas

 if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } NSURL *newURL = [self newStoreURL]; // shared group container NSURL *oldURL = [self oldStoreURL]; // app's Documents directory __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:newURL.path] && [fileManager fileExistsAtPath:oldURL.path]) { NSLog(@"performing migration..."); NSPersistentStore *oldStore = [__persistentStoreCoordinator persistentStoreForURL:oldURL]; NSError *migrateError = nil; NSPersistentStore *newStore = [__persistentStoreCoordinator migratePersistentStore:oldStore toURL:newURL options:[self iCloudPersistentStoreOptions] withType:NSSQLiteStoreType error:&migrateError]; if (!newStore) { NSLog(@"Error migrating store: %@", [migrateError localizedDescription]); } } // rest of setup return __persistentStoreCoordinator; 

D'après ce que je peux dire, il semble que vous soyez à peu près là si la logique de migration que vous avez affichée fonctionne. Ce qu'il semble vous manquer est un else if gérer le cas où vous n'avez pas de magasin persistant. Ce qui suit devrait gérer ce cas.

 if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } NSURL *newURL = [self newStoreURL]; // shared group container NSURL *oldURL = [self oldStoreURL]; // app's Documents directory __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:newURL.path] && [fileManager fileExistsAtPath:oldURL.path]) { NSLog(@"performing migration..."); NSPersistentStore *oldStore = [__persistentStoreCoordinator persistentStoreForURL:oldURL]; NSError *migrateError = nil; NSPersistentStore *newStore = [__persistentStoreCoordinator migratePersistentStore:oldStore toURL:newURL options:[self iCloudPersistentStoreOptions] withType:NSSQLiteStoreType error:&migrateError]; if (!newStore) { NSLog(@"Error migrating store: %@", [migrateError localizedDescription]); } } else if (![fileManager fileExistsAtPath:newURL.path]) { if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:newURL options:[self iCloudPersistentStoreOptions] error:&error]) { NSLog(@"Error adding persistent store: %@", [error localizedDescription]); } } // rest of setup return __persistentStoreCoordinator;