Delete model from Realm

2019-09-11 12:45发布

问题:

I am trying to remove a model from Realm. I appears there's a straightforward way to do it in Java with

realm.getSchema().remove(className)

It doesn't appear there is an option in Swift 3 other than to remove the model from the App and then migrate the data, or delete the entire Realm file.

To clarify, when I open the Realm Browser I have three models

Dog 2
Person 4
Test 0

and I want to remove just the Test model via code. There doesn't appear to be any way to remove it via the Browser either.

Perhaps I overlooked something in the docs?

回答1:

No, you haven't overlooked anything in the docs.

It is not possible to modify the schema of a Realm file in the Objective-C/Swift SDKs without triggering a migration. In which case, you can use Migration.deleteData(forType:) to delete the object schema from the Realm.

Additionally, if you want to explicitly ensure that Test isn't added to your Realm file in the first place, you can explicitly define that in your Realm configuration.



标签: swift3 realm