2010-09-02 3 views
3

Je souhaite migrer de mon modèle de données version1 vers version2, mais une fois la migration terminée, je souhaite effectuer un code de migration personnalisé. Comment saurai-je si/quand la migration a lieu? Existe-t-il une méthode ou une notification de délégué de migrationHasCompleed?Données de base: post-migration, code de migration supplémentaire

Par souci d'intérêt: Le code de migration personnalisé que je souhaite effectuer redimensionne les png dans la base de données.

+0

vérifier: http://stackoverflow.com/questions/3025742/detecting-a-lightweight-core-data-migration – flypig

+0

également vérifier ceci: http://www.ioscodingtips.com/lightweight-migration -with-core-data/ – flypig

Répondre

0

Eh bien, vous pouvez exécuter ce code juste après la migration se produit, votre configuration persistante coordinateur de magasin:

+ (NSPersistentStoreCoordinator *)persistentStoreCoordinatorForStore:(NSString *)store { 
    NSURL *storeUrl = [NSURL fileURLWithPath:[[[self class] applicationDocumentsDirectory] stringByAppendingPathComponent:store]]; 

    NSError *error = nil; 
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[self class] managedObjectModel]]; 

    // Check for model changes without trying to update 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { 
     // Set the automatic update options for the current model 
     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
           nil]; 

     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 

      NSLog(@"Error opening the database. Deleting the file and trying again."); 

      //delete the sqlite file and try again 
      [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil]; 
      if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 
      { 
       /* 
       Replace this implementation with code to handle the error appropriately. 

       abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

       Typical reasons for an error here include: 
       * The persistent store is not accessible 
       * The schema for the persistent store is incompatible with current managed object model 
       Check the error message to determine what the actual problem was. 
       */ 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       abort(); 
      } 
     } else { // The model was updates successfuly 
      // Implement here your custom migration code 
     } 

    }  

    return [persistentStoreCoordinator autorelease]; 
} 

Cheers,

VFN

+0

Merci, je vais y aller ... – user139816

+0

Depuis les docs d'Apple: "Avant d'ouvrir un magasin, vous utilisez isConfiguration: compatibleWithStoreMetadata: pour vérifier si son schéma est compatible avec le modèle du coordinateur. Vous pouvez simplement utiliser addPersistentStoreWithType: configuration: URL: options: error: pour vérifier si la migration est nécessaire, mais ceci est une opération lourde et inefficace à cette fin. " – cocoafan

2

Pour référence, vous pouvez aussi tester avancer si une migration est nécessaire, ce qui serait probablement plus propre.

NSError *error; 
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType 
                          URL:storeURL 
                         error:&error]; 
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel]; 
BOOL migrationRequired = ![destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]; 

// Now add persistent store with auto migration, and do the custom processing after 
Questions connexes