SQLite.swift icon indicating copy to clipboard operation
SQLite.swift copied to clipboard

Add combine support (change listener)

Open ursusursus opened this issue 1 year ago • 4 comments

Reactive database is needed in reactive architecture. I don't see any Combine bindings provided. Is there some sort of data change listener I could adapt myself to Combine?

ursusursus avatar Jul 25 '22 12:07 ursusursus

SQLite is certainly capable of database observation. Check out GRDB's ValueObservation for ready-made Combine publishers.

// Connect to the database
let dbQueue = ... 

// Define an observation for the tracked value(s).
let observation = ValueObservation.tracking { db in
    // Fetch anything you want, from as many tables as needed
    return ...
}

// Turn the observation into a Combine publisher
let publisher = observation.publisher(in: dbQueue)

// Be notified of all changes, until the subscription is cancelled.
let cancellable = publisher.sink(
    receiveCompletion: { completion in ... },
    receiveValue: { value in
        print("Fresh database value: \(value)")
    })

groue avatar Jul 25 '22 18:07 groue

@ursusursus Can you explain a bit more what you're after?

jberkel avatar Jul 26 '22 06:07 jberkel

@jberkel I basically want observable queries, that is to get the current result of the query as first emit, and then subsequent emits as the underlying data changes. In other words, query over time.

ursusursus avatar Jul 26 '22 10:07 ursusursus

Alright. See #686 for a related discussion, which unfortunately fizzled out (there was a user who mentioned opening a PR, but nothing materialized).

jberkel avatar Jul 26 '22 12:07 jberkel