groue / GRDBCombine

GRDB ❀️ Combine
MIT License
223 stars 16 forks source link

Refreshing request #18

Closed skrew closed 4 years ago

skrew commented 4 years ago

Hi Gwendal,

One day, one question πŸ˜‡

Since you wanted concrete cases of use... πŸ˜…

I'm still evaluating SwiftUI / GRDBCombine, and i have 2 questions (in fact, one question, it's the same topic)

I've tried several methods but none of them work, I'm blocked because when I do a new query and plug to DatabasePublished(publisher), it loses (i think) the link with the @ObservedObject variable of the view, which is normal.

I haven't found a way to redo a query for the same @ObservedObject variable.

On the other hand, the associations works, and that's great !!! πŸ‘πŸ»

Thanks

groue commented 4 years ago

Hello @skrew!

You're entering uncharted territory, I admit. I lack experience about SwiftUI so I can't really answer your first question.

I may help for your second question, though. It its current implementation, @DatabasePublished tracks a single request, which can not change. If the request changes over time, you may have to put DatabasePublished aside, and instead create a publisher of requests (driven by your UI), and convert it to a publisher of database values with switchToLatest. Gross untested sample code:

let request = ... // Publisher of QueryInterfaceRequest<Channel>
let channels = request.switchToLatest {
    $0.observationForAll().publisher(in: dbQueue)
}
channels.sink(
    receiveCompletion: { _ in },
    receiveValue: { channel in print("fresh channels: \(channels)") })

This is basically the technique used for the "toggle ordering" button in the RxGRDB demo app.

Maybe @DatabasePublished could be made able to track a publisher of requests instead of a single request. Your own experience, suggested changes, maybe motivated pull requests will be welcomed!

groue commented 4 years ago

No news is good news: I assume the previous answer was able to unblock you.