如何實現COCaData的版本遷移

第一步:選中你要遷移的cocaData文件,然後依次點擊點擊上方的editor、Add Model Version。然後創建新的cocaData文件,文件名可以自己改寫。


第二步:改變當前的版本,操作爲,點擊新創建的cocaData文件,然後點擊右邊的Model Version,選擇爲當前的文件名


第三步:找到下面這個方法

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{

    if (_persistentStoreCoordinator != nil) {

        return _persistentStoreCoordinator;

    }

    

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Projec_B1.sqlite"];

    

    NSError *error = nil;

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

第四步: 創建字典:


   NSDictionary *optionsDic = @{NSInferMappingModelAutomaticallyOption:@(YES), NSMigratePersistentStoresAutomaticallyOption:@(YES)};


第五步: 然後把options的參數改爲創建的字典:

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDic error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        abort();

    }    

    return _persistentStoreCoordinator;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章