美文网首页征服Swift
Realm Swift 数据库简单迁移

Realm Swift 数据库简单迁移

作者: Luyc_Han | 来源:发表于2017-10-16 09:30 被阅读24次

Local Migrations
注意在头文件引入import Realm

// Inside your application(application:didFinishLaunchingWithOptions:)

let config = Realm.Configuration(
    // Set the new schema version. This must be greater than the previously used
    // version (if you've never set a schema version before, the version is 0).
    schemaVersion: 1,

    // Set the block which will be called automatically when opening a Realm with
    // a schema version lower than the one set above
    migrationBlock: { migration, oldSchemaVersion in
        // We haven’t migrated anything yet, so oldSchemaVersion == 0
        if (oldSchemaVersion < 1) {
            // Nothing to do!
            // Realm will automatically detect new properties and removed properties
            // And will update the schema on disk automatically
        }
    })

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()

一般来说上面的迁移就可以如果想更新值可以使用下面的

// Inside your application(application:didFinishLaunchingWithOptions:)

Realm.Configuration.defaultConfiguration = Realm.Configuration(
    schemaVersion: 1,
    migrationBlock: { migration, oldSchemaVersion in
        if (oldSchemaVersion < 1) {
            // The enumerateObjects(ofType:_:) method iterates
            // over every Person object stored in the Realm file
            migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
                // combine name fields into a single field
                let firstName = oldObject!["firstName"] as! String
                let lastName = oldObject!["lastName"] as! String
                newObject!["fullName"] = "\(firstName) \(lastName)"
            }
        }
    })

以上描述并不清晰在此附上官方文档以供查阅
Realm Stwift

相关文章

  • Swift Realm数据库 本地迁移

    在我们开发中不免会遇到使用数据库的情况,如果使用swift开发并且数据库使用了Realm的时候,这篇简单的本地迁移...

  • Realm Swift 数据库简单迁移

    Local Migrations注意在头文件引入import Realm 一般来说上面的迁移就可以如果想更新值可以...

  • IOS DB存储之Realm.swift (一) 使用篇

    @[TOC](IOS DB存储之Realm.swift ) Swift-Realm数据库的使用详解 下载本篇博客的...

  • iOS - objective-c中realm的简单实用

    在swift项目的开发中,realm是炙手可热的移动端数据库,我们一起来简单了解下realm[https://re...

  • Swift-Realm数据库的使用详解

    Swift-Realm数据库的使用详解 概述 Realm 是一个跨平台的移动数据库引擎,其性能要优于 Core D...

  • 持久化方案-Realm(一):基本使用

    什么是Realm Realm是一个跨平台移动数据库引擎,支持iOS、OS X(Objective-C和Swift)...

  • Android之Realm

    Realm简介 realm是一个跨平台移动数据库引擎,支持iOS、OS X(Objective-C和Swift)以...

  • Realm - swift

    realm 介绍及安装 简介 Realm 是一个跨平台的移动终端数据库,支持 iOS(Swift 和 Object...

  • 2018-04-16

    1.Realm介绍 realm是一个跨平台移动数据库引擎,支持iOS、OS X(Objective-C和Swift...

  • 浅谈RealmSwift

    Realm Swift版 Realm swift中文文档地址:https://realm.io/cn/docs/s...

网友评论

    本文标题:Realm Swift 数据库简单迁移

    本文链接:https://www.haomeiwen.com/subject/zwzhuxtx.html