首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改类的属性,而不必使用领域删除应用程序

如何更改类的属性,而不必使用领域删除应用程序
EN

Stack Overflow用户
提问于 2016-02-26 18:17:55
回答 1查看 976关注 0票数 3

目前,我正在用Swift编写一个使用领域的程序。我对iOS开发相当陌生,但我对领域的理解是,当您更改存储在领域中的类时,需要从设备中删除应用程序,以摆脱持久化数据。不幸的是,我已经手动输入了一个相当大的数据库到应用程序。

目前,我需要在类中更改属性的名称,但将来可能需要添加属性。更新领域存储的最佳方法是什么,这样我就不需要删除应用程序了?

这是我的模型之一:

代码语言:javascript
复制
class Device: Object {

   dynamic var name = ""
   dynamic var id = ""
   dynamic var os = ""
   dynamic var currentUser: User?
   dynamic var dateStamp = NSDate()
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-29 14:12:31

您可以添加一个迁移,如我们的医生中所示,并使用它来接管新属性的旧值:

Objective-C

代码语言:javascript
复制
// Inside your [AppDelegate didFinishLaunchingWithOptions:]

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// 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).
config.schemaVersion = 1;

// Set the block which will be called automatically when opening a Realm with a
// schema version lower than the one set above
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
        // The -enumerateObjects:block: method iterates
        // over every Device object stored in the Realm file
        [migration enumerateObjects:Device.className
                      block:^(RLMObject *oldObject, RLMObject *newObject) {
            // e.g. Rename 'os' to 'operatingSystem'
            newObject[@"operatingSystem"] = oldObject[@"os"]
        }];
    }
};

// Tell Realm to use this new configuration object for the default Realm
[RLMRealmConfiguration setDefaultConfiguration:config];

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
[RLMRealm defaultRealm];

斯威夫特(与王国斯威夫特)

https://realm.io/docs/swift/latest/#performing-a-migration

代码语言:javascript
复制
// 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) {
        // The enumerate(_:_:) method iterates
        // over every Device object stored in the Realm file
        migration.enumerate(Device.className()) { oldObject, newObject in
            // e.g. Rename 'os' to 'operatingSystem'
            newObject["operatingSystem"] = oldObject["os"]
        }
    }
  })

// 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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35658916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档