Lory Huz Lory Huz - 28 days ago 19
Swift Question

Realm, avoid to store some property

I need to implement a protocol in my User model, which need to have some special properties. But I'd like to avoid them to be stored/persisted in Realm database.

I didn't see in the documentation if there was a keyword for this. Did there is a trick to avoid saving some properties ?

public final class User: Object, Mappable, AvatarImageViewDataSource {

dynamic var id: Int = 0
dynamic var desc: String? = nil
dynamic var email: String? = nil
dynamic var firstName: String? = nil
dynamic var lastName: String? = nil

...

public var myPropertyIDontWantToSave: String? = nil // I don't want this to be stored

Answer

Check out the RealmSwift docs about Ignored properties. There's is some sample code:

class Person: Object {
  dynamic var tmpID = 0
  var name: String { // read-only properties are automatically ignored
    return "\(firstName) \(lastName)"
  }
  dynamic var firstName = ""
  dynamic var lastName = ""

  override static func ignoredProperties() -> [String] {
    return ["tmpID"]
  }
}
Comments