objectbox / objectbox-swift

Swift database - fast, simple and lightweight (iOS, macOS)
https://swift.objectbox.io
Apache License 2.0
451 stars 30 forks source link

QueryPublisher unsubscribes after one event #86

Open Feuerwerk opened 1 week ago

Feuerwerk commented 1 week ago

Is there an existing issue?

Build info

Steps to reproduce

let query: Query<Person> = ......
query.publisher
                    .replaceError(with: [])
                    /*.assign(to: \.persons, on: viewModel)*/
                    .sink(receiveValue: { persons in
                        viewModel.persons = persons
                    })
                    .store(in: &cancellables)

Expected behavior

I expected that viewModel.persons will update continuously when I add / change a record that will match my query.

Actual behavior

After the initial query result is consumed by the Subscriber, QueryPublisher will unsubscribe because various Subscriber implementations like sink(), assign(to:) or onReceive() of a SwiftUI view will return Subscriber.Demand.none which causes QueryPublisher to signal completion to the Subscriber and shut down.

kemkriszt commented 3 days ago

I'm also curious of how to handle this, I ended up creating a custom subscriber that after every receive requests unlimited but it seems like an ugly hack to me