iOS 10 coreData 版本遷移

2021-07-28 04:48:29 字數 2193 閱讀 9707

以前的方法需要在

1設定新版本

表示支援版本遷移,以及版本遷移後自動設定對映關係

就可以用了

//版本遷移要在option加字典  

if (![_persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:storeurl

error:&error])

ios 10 以後需要在建立持久化助理 

//2、建立持久化助理 //

利用模型物件建立助理物件

nspersistentstorecoordinator

*store = [[

nspersistentstorecoordinator

alloc

] initwithmanagedobjectmodel

:model];

//coredata

版本遷移

//    [nsnumber numberwithbool:yes] = @(yes);

nsdictionary

*options =@;

//設定資料庫相關資訊

[store

addpersistentstorewithtype

:nssqlitestoretype

configuration

:nil

url:sqlurl

options

:options

error

:nil];

//3、建立上下文

nsmanagedobjectcontext

*context = [[

nsmanagedobjectcontext

alloc

] 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簡單實用  

////   

//   

//coredata

封裝//    //

版本公升級

// 

//coredata

相容ios9

和ios10

CoreData版本遷移

如果已經建立好了coredata資料庫,當實體中多了乙個屬性的時候,我們直接去更改資料表中實體,為其增添屬性會發生錯誤,此時需要通過版本更新的方式解決這個問題.1.新增乙個model version檔案 2.設定使用以後要使用的版本 4.刪除之前版本中person實體對應的類,用新版本的person...

iOS基礎 CoreData 總結

coredata 資料持久化框架是cocoa api的一部分,它允許按照 實體 屬性 值 模型組織資料,並以xml 做mac os用 二進位制,或者sqlite資料檔案的格式持久化資料.coredata主要提供 物件 關係對映 orm 功能,把oc物件轉化為資料儲存到檔案,也可以資料轉化成oc物件....

IOS資料儲存之 Core Data

前言 構成 1 nsmanagedobjectcontext 被管理的資料上下文 操作實際內容 操作持久層 作用 插入資料,查詢資料,刪除資料 2 nsmanagedobjectmodel 被管理的資料模型 資料庫所有 或資料結構,包含各實體的定義資訊 作用 新增實體的屬性,建立屬性之間的關係 3 ...