Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] change gets swollen when using synchronous first element #74

Open
icanzilb opened this issue Apr 1, 2017 · 3 comments
Open

[bug] change gets swollen when using synchronous first element #74

icanzilb opened this issue Apr 1, 2017 · 3 comments
Assignees
Labels
Milestone

Comments

@icanzilb
Copy link
Member

icanzilb commented Apr 1, 2017

As described here:
realm/realm-swift#4761 (comment)

Demo code:

class Test: Object {
    dynamic var name = ""
    override var description: String {
        return "[name => '\(name)']"
    }
}

class ViewController: UIViewController {

    private let bag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()

        test()
    }

    var token: NotificationToken?

    func test() {
        let realm = try! Realm()
        let obj = Test()

        try! realm.write {
            realm.deleteAll()
            realm.add(obj)
        }

        let tests = realm.objects(Test.self)

        DispatchQueue.global(qos: .background).async {
            let realm = try! Realm()
            let obj = realm.objects(Test.self).first!

            try! realm.write {
                obj.name = "Test0"
            }
        }

        Observable.array(from: tests)
            .subscribe(onNext: { value in
                print("rx next: \(flatten(value))")
            })
            .addDisposableTo(bag)

        token = tests.addNotificationBlock { changes in
            switch changes {
            case .initial(let tests):
                print("realm initial: \(flatten(tests))")
            case .update(let tests, _, _, _):
                print("realm changed: \(flatten(tests))")
            default: break
            }
        }

    }

}
@tristangrichard
Copy link

Anything new on this? Any workaround?

@freak4pc
Copy link
Member

freak4pc commented Nov 24, 2020 via email

@anton-plebanovich
Copy link

Demo code may be even simplier:

let realm: Realm = try! Realm()
try! realm.write {
    realm.add(MyObject())
}

let results = realm.objects(MyObject.self)

_ = Observable
    .array(from: results, synchronousStart: true, on: .main)
    .subscribe(onNext: { objects in
        print("Objects count: \(objects.count)")
    })

try! realm.write {
    realm.delete(results)
}

Expected output:

Objects count: 1
Objects count: 0

Actual output:

Objects count: 1

The issue is that we need to check both collections for equality to decide if we should or should not emit an event in initial which is cumbersome and close to impossible in practice.

RxRealm is just a wrapper around Realm and shouldn't bring unexpected side effects like this one. The other issue is that synchronousStart doesn't care about queue parameter so the initial event might even come from an unexpected queue. I really got hurt by this one spending days investigating so my proposal is to remove the synchronousStart parameter at all and do everything async. This one will be a major change though.

If that's approved I'll create a pull request

@JoeMatt JoeMatt self-assigned this Jan 22, 2022
@JoeMatt JoeMatt added this to the 5.0.5 milestone Jan 22, 2022
@JoeMatt JoeMatt changed the title change gets swollen when using synchronous first element [bug] change gets swollen when using synchronous first element Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants