Delete a Realm model with Swift

谁说胖子不能爱 提交于 2019-12-12 02:25:08

问题


I need to remove old, empty models from a Realm Cocoa database.

There seems to be a way to do it in Java, but not in Swift. Is that correct?

If you remove a property and initiate a migration Realm will remove the corresponding column in the table:

class Dog: Object {
  dynamic var name = ""
  // dynamic var age = 0
}

But, if you remove the model definition entirely, the migration does not remove the table:

// class Dog: Object {
//   dynamic var name = ""
//   dynamic var age = 0
// }

Here's a screenshot from Realm Browser showing the empty tables I want to delete:


回答1:


You can call Migration.deleteData(_:) within your migration block to specify that the named class should be completely removed from your Realm file.




回答2:


For those who are working on JavaScript:

migration: (oldRealm, newRealm) => {
  if (oldRealm.schemaVersion < *version*) {
    newRealm.deleteModel('MODEL-NAME')
  }
}


来源:https://stackoverflow.com/questions/38407452/delete-a-realm-model-with-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!