Lory Huz
Lory Huz

Reputation: 1508

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

Upvotes: 18

Views: 7920

Answers (1)

Orlando
Orlando

Reputation: 1529

Check out the RealmSwift docs about Ignoring properties. There is some sample code included on that section:

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"]
  }
}

Upvotes: 32

Related Questions