iOS 10 coreData 版本迁移

    xiaoxiao2021-03-25  80

     以前的方法需要在

    1设置新版本

     在Appdelegate.m的 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator方法里的这个代码的选项参数里面添加一个字典 @ {NSMigratePersistentStoresAutomaticallyOption:@ YES,NSInferMappingModelAutomaticallyOption:@YES}  表示支持版本迁移,以及版本迁移后自动设置映射关系 就可以用了

    //版本迁移要在option加字典 if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES} error:&error]) iOS 10 以后需要在创建持久化助理  NSPersistentStoreCoordinator 时设置  不需要再Appdelegate.m里面设置(第一步创建版本时和以前的方法一致)

    //2、创建持久化助理

        //利用模型对象创建助理对象

        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:model];

        //coreData版本迁移

      //    [NSNumber numberWithBool:YES] = @(YES);

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

        //设置数据库相关信息

        [store addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:sqlUrl options:optionserror:nil];

        //3、创建上下文

        NSManagedObjectContext *context = [[NSManagedObjectContextalloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

        

        //关联持久化助理

        [context setPersistentStoreCoordinator:store];

       self.mangerContext = context;

    coreData

    1 优缺点比较

    // SQLite

    //    1、基于C接口,需要使用SQL语句,代码繁琐

    //    2、在处理大量数据时,表关系更直观

    //    3、在OC中不是可视化,不易理解

    //CoreData

    //    1、可视化,且具有undo/redo能力

    //    2、可以实现多种文件格式:

    //    * NSSQLiteStoreType     sqlite

    //    * NSBinaryStoreType     二进制

    //    * NSInMemoryStoreType

    //    * NSXMLStoreTyp          xml

    //    3、苹果官方API支持,与iOS结合更紧密

    1  步骤

      //      1.创建模型文件[相当于一个数据库] NSManagedObjectModel

        //    2.添加实体[一张表]                     

        //    3.创建实体类 [相当模型--表结构]

        //    4.生成上下文关联模型文件生成数据库  NSManagedObjectContext

         //

    使用博客:

     // coredata简单实用  http://www.jianshu.com/p/69f55de1f6a9

      //http://blog.csdn.net/q199109106q/article/details/8563438/

        //    http://www.cnblogs.com/guogangj/p/3650799.html

        //    http://www.cnblogs.com/lihaibo-Leao/p/5508006.html

     //coredata封装  http://www.cnblogs.com/panda1024/p/6219726.html

    //    http://www.jianshu.com/p/880dd63c5f5e

        //版本升级

      //  http://www.th7.cn/Program/IOS/201609/963792.shtml

        //CoreData兼容iOS9iOS10http://blog.csdn.net/u013263917/article/details/53277909

    转载请注明原文地址: https://ju.6miu.com/read-16771.html

    最新回复(0)