Realm数据库版本迁移
生活随笔
收集整理的這篇文章主要介紹了
Realm数据库版本迁移
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
當(dāng)你和數(shù)據(jù)庫(kù)打交道的時(shí)候,你需要改變數(shù)據(jù)模型(Model),但是因?yàn)镽ealm中的數(shù)據(jù)模型被定義為標(biāo)準(zhǔn)的Objective-C interfaces,要改變模型,就像改變其他Objective-C interface一樣輕而易舉。舉個(gè)例子,假設(shè)有個(gè)數(shù)據(jù)模型Person:
@interface Person : RLMObject @property NSString *firstName; @property NSString *lastName; @property int age; @end 復(fù)制代碼當(dāng)我們想添加一個(gè)字段fullName屬性而不是first和last names時(shí),我們可以這樣做:
@interface Person : RLMObject @property NSString *fullName; @property int age; @end 復(fù)制代碼接下來(lái)執(zhí)行遷移:
// Inside your [AppDelegate didFinishLaunchingWithOptions:]RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; config.schemaVersion = 1; config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {//如果從沒(méi)遷移過(guò),oldSchemaVersion == 0if (oldSchemaVersion < 1) {// The enumerateObjects:block: method iterates// over every 'Person' object stored in the Realm file[migration enumerateObjects:Person.classNameblock:^(RLMObject *oldObject, RLMObject *newObject) {// 設(shè)置新增屬性的值newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@",oldObject[@"firstName"],oldObject[@"lastName"]];}];} }; [RLMRealmConfiguration setDefaultConfiguration:config]; 復(fù)制代碼在遷移的過(guò)程中重命名屬性名稱
// Inside your [AppDelegate didFinishLaunchingWithOptions:]RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; config.schemaVersion = 1; config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {// We haven’t migrated anything yet, so oldSchemaVersion == 0if (oldSchemaVersion < 1) {// The renaming operation should be done outside of calls to `enumerateObjects:`.[migration renamePropertyForClass:Person.className oldName:@"yearsSinceBirth" newName:@"age"];} }; [RLMRealmConfiguration setDefaultConfiguration:config]; 復(fù)制代碼官方eg:
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; config.schemaVersion = 2; config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {// enumerateObjects:block: 遍歷了存儲(chǔ)在 Realm 文件中的每一個(gè)“Person”對(duì)象[migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {// 只有當(dāng) Realm 數(shù)據(jù)庫(kù)的架構(gòu)版本為 0 的時(shí)候,才添加 “fullName” 屬性if (oldSchemaVersion < 1) {newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]];}// 只有當(dāng) Realm 數(shù)據(jù)庫(kù)的架構(gòu)版本為 0 或者 1 的時(shí)候,才添加“email”屬性if (oldSchemaVersion < 2) {newObject[@"email"] = @"";}// 替換屬性名if (oldSchemaVersion < 3) { // 重命名操作應(yīng)該在調(diào)用 `enumerateObjects:` 之外完成 [migration renamePropertyForClass:Person.className oldName:@"yearsSinceBirth" newName:@"age"]; }}]; }; [RLMRealmConfiguration setDefaultConfiguration:config]; // 現(xiàn)在我們已經(jīng)成功更新了架構(gòu)版本并且提供了遷移閉包,打開(kāi)舊有的 Realm 數(shù)據(jù)庫(kù)會(huì)自動(dòng)執(zhí)行此數(shù)據(jù)遷移,然后成功進(jìn)行訪問(wèn) [RLMRealm defaultRealm]; 復(fù)制代碼總結(jié)
以上是生活随笔為你收集整理的Realm数据库版本迁移的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: css背景图宽度只适应,高度不变
- 下一篇: App Store应用脱壳