stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

Add combine support (change listener) #1147

Open ursusursus opened 1 year ago

ursusursus commented 1 year ago

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?

groue commented 1 year ago

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)")
    })
jberkel commented 1 year ago

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

ursusursus commented 1 year ago

@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.

jberkel commented 1 year ago

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