简体   繁体   中英

RxSwift Variable with Realm object storing

I am having some issues understanding if there is any support to combine RxSwift with Realm, as Realm explicitly states the supported variable types.

Is it possible instead of the following.

import RealmSwift

class Dog: Object {
  dynamic var name = ""
}

To do something like this.

import RealmSwift
import RxSwift

class Dog: Object {
  var name = Variable<String>("")
}

I have done some googling and haven't found any up to date information about extensions or other solutions to do this. Would appreciate any help or just pointing in the right direction.

How about using KVO rx_observe() instead? Like the following:

dog
    .rx_observe(String.self, "name")
    .subscribeNext { name in
        print("string: \(name)")
}

But there is a limitation for Realm object that has not been persisted with KVO.

For the persisted object, it's no limitation for observing.

NOTE:

Observing properties of standalone instances of Object subclasses works just like with any other dynamic property, but note that you cannot add an object to a Realm (with realm.add(obj) or other similar methods) while it has any registered observers.

https://realm.io/docs/swift/latest/#key-value-observation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM