码迷,mamicode.com
首页 > 其他好文 > 详细

CoreData 轻量级迁移

时间:2015-02-17 21:02:21      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

CoreData 轻量级迁移

// CoreData 轻量级迁移
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
 
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData_LightweightMigration.sqlite"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application‘s saved data.";
    
    /*! 主要添加的代码块*/
    // NSMigratePersistentStoresAutomaticallyOption
    // NSInferMappingModelAutomaticallyOption
    // 设置为YES
    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]) {
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application‘s saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return _persistentStoreCoordinator;
}

 

CoreData 轻量级迁移

标签:

原文地址:http://www.cnblogs.com/advacedprogrammer/p/4295427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!