1

I have read the following question on Stack Overflow (What is the best way to connect Realm and SwiftBond), which is unfortunately 2 years old. I am in the same position where I would like to create an observable instance of a Realm object, whereby the Realm object will get updated from the UI, and can then be written to Realm.

From what I have read and understood, I think that Observable(object:keyPath:) no longer exists in Bond v6, but I can't quite figure out what the alternative is, though I think it has something to do with dynamic(keyPath:ofType:).

I am struggling to find an example or any documentation which would allow me to do the Bond 6 version of the following:

class Dog: Object {
  dynamic var name = ""
  dynamic var birthdate = NSDate(timeIntervalSince1970: 1)
}

extension Dog {

  class ObservableDog {
    let name: Observable<String>
    let birthdate: Observable<NSDate>

    init(dog: Dog) {
      name = Observable(object: dog, keyPath: "name")
      birthdate = Observable(object: dog, keyPath: "birthdate")
    }
  }

  func observableVariant() -> Dog.ObservableDog {
    return ObservableDog(dog: self)
  }
}

let myDog = Dog().observableVariant()

myDog.name.observe { newName in
  print(newName)
}

myDog.name.bindTo(nameLabel.bnd_text)

realm.write {
  myDog.name.value = "Jim"
}

I'm now on my second day of attempting this, so I'm hoping that someone will be able to help.

Thanks in advance.

0 Answers0