领域迁移不起作用

领域迁移不起作用

本文介绍了领域迁移不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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()

此内容已放入application(application:didFinishLaunchingWithOptions:)

This was put in application(application:didFinishLaunchingWithOptions:)

在测试程序中,我更改了对象中的字段.我想删除数据库中的所有内容,然后移到新的字段类型.我已经从文档中复制了上面的代码,但它似乎无能为力.我仍然遇到这些错误:

In my test program, I have changed the fields in my object. I would like to remove everything in the database, and move to the new field types. I've copied the code above from the documentation, but it appears to do nothing. I still get these errors:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Migration is required due to the following errors:
- Property types for 'unit' property do not match. Old type 'string', new type 'int'
- Property 'reps' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required due to the following errors:
- Property types for 'unit' property do not match. Old type 'string', new type 'int'
- Property 'reps' has been added to latest object model.}: file   /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/

有什么想法吗?

推荐答案

只要您仅在本地开发,我建议您重置Realm数据库,而不要进行迁移.如果您已经将应用程序的版本与其他架构一起发布,并且希望保留用户数据,则可以采用迁移方式.

As long as you're in local development only, I'd recommend to reset your Realm database instead of doing a migration. Migrations are the way to go, if you have already shipped a version of your app with another schema and want to keep user data.

您可以通过从模拟器或设备中删除应用程序来删除数据库.或者,您可以使用 NSFileManager 在访问数据库之前删除Realm文件.

You can delete the database by deleting the app from the simulator or the device.Alternatively you can use NSFileManager to delete the Realm file before accessing the database.

let defaultPath = Realm.Configuration.defaultConfiguration.path!
try NSFileManager.defaultManager().removeItemAtPath(defaultPath)

这篇关于领域迁移不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:06