-
-
Notifications
You must be signed in to change notification settings - Fork 731
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
ValueObservation filtering by foreign key #516
Comments
Hello again @mackuba :-) Thanks for the detailed report! You are experiencing normal behavior: let request = gallery1.photos
let builder: ((Database) throws -> ([Photo])) = { db in
return try request.fetchAll(db)
}
let observation = ValueObservation.tracking([request], fetch: builder) This code observes Unlike #514, this is not bug in SQLite. Here we are facing a hard-coded limitation: SQLite can not spot individual rows unless they are identified by their integer primary key: Photo.filter(Column("galleryId") == 1) // not precise
Photo.filter(Column("position") == 0) // not precise
Photo.filter(Column("id") == 1) // precise Consequence: GRDB has to trigger the ValueObservation for any change that happens to the photo table. In order to avoid undesired changes notifications, you have to instruct GRDB to perform deduplication of identical results. You have two ways to do so:
|
Ah, I see... Ok, |
OK @mackuba! I hope this issue will help other users who face the same surprise. Happy GRDB! |
Hello @groue , we ran into this issue today. I notice that |
Hello @dawilliams-gpsw. Sample code was given for a former GRDB version indeed. Here is the modern form (GRDB 6): // Formerly ValueObservation.trackingAll(request)
let observation = ValueObservation.tracking { db in try request.fetchAll(db) } As with all Swift closures, you can use the short form: // Short version
let observation = ValueObservation.tracking(request.fetchAll) |
Ah, yes, that is indeed the form we're using. In the closure, we're using something like:
and we are seeing observations triggered for identifiers that weren't |
Oh, yes. When SQLite notifies database changes, it identifies inserted/updated/deleted rows with their This explains the extra change notifications you see. Using If you're interested, check let request = Parent
.filter(id: someIdentifier)
.joining(required: Parent.children)
let region = try request.databaseRegion(db)
print(region) If the code above prints For some requests, you'll see |
Hi, it's me again :) With the exact same observation actually as in #514.
I was writing some tests today for this code, and I've noticed that even though I'm trying to observe only changes in the photos/sequences set for a specific gallery, the fetch block is fired whenever a photo/sequence is added to any gallery. This normally doesn't happen in the app since only one gallery is being built at a time in the UI, but I created such situation in the test.
Here's some simplified sample code that seems to be reproducing this:
And I get this:
The text was updated successfully, but these errors were encountered: