RxSwiftCommunity / RxRealm

RxSwift extension for RealmSwift's types
MIT License
1.15k stars 265 forks source link

Object is instantly deleted after being created #118

Closed Blackening999 closed 5 years ago

Blackening999 commented 5 years ago

Hello

I can't understand why Realm behaves like this

I have logged in, confirmed email and then simply on the controller viewDidLoad trying to understand what the hell is going :)

As an output of the code below, first, I see changes as record inserted, shortly after as deleted!

No clue why it's happening

UserItem

class UserItem: Object {
  @objc dynamic var uid: Int = 0
  @objc dynamic var owner: String = ""
  @objc dynamic var email: String = ""
  @objc dynamic var added: Date = Date()

  override class func primaryKey() -> String? {
    return "uid"
  }
}

extension UserItem: IdentifiableType {
  var identity: Int  {
    return self.isInvalidated ? 0 : uid
  }
}

ViewController

let realm = try! Realm(configuration: SyncUser.current!.configuration())

    let users = realm.objects(UserItem.self)

    Observable.changeset(from: users)
      .subscribe(onNext: { results, changes in
        if let changes = changes {
          // it's an update
          print(results)
          print("deleted: \(changes.deleted)")
          print("inserted: \(changes.inserted)")
          print("updated: \(changes.updated)")
        } else {
          // it's the initial data
          print(results)
        }
      })

    let user = UserItem()

    user.email = "someemail@gmail.com"
    user.owner = SyncUser.current!.identity!

    try! realm.write {
      user.uid = (realm.objects(UserItem.self).max(ofProperty: "uid") ?? 0) + 1
      realm.add(user)
    }
Blackening999 commented 5 years ago

Related to https://github.com/RxSwiftCommunity/RxRealm/issues/119 The issue were exactly because of partial sync weren't handled properly