-
-
Notifications
You must be signed in to change notification settings - Fork 722
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 doesn't trigger for fetchCount #954
Comments
Hello @steipete, Observing Sample codeimport PlaygroundSupport
import Combine
import Foundation
import GRDB
PlaygroundPage.current.needsIndefiniteExecution = true
// Database setup
let path = NSTemporaryDirectory().appending("test.sqlite")
let dbPool = try DatabasePool(path: path)
try dbPool.write { db in
try db.execute(sql: "DROP TABLE IF EXISTS tweet")
try db.create(table: "tweet") { t in
t.autoIncrementedPrimaryKey("id")
}
}
// Minimal models
struct Tweet: TableRecord { }
struct Statistics {
var tweetCount: Int
}
// The publisher
let publisher = ValueObservation.tracking { db in
return Statistics(tweetCount: try Tweet.all().fetchCount(db))
}
.publisher(in: dbPool).eraseToAnyPublisher()
// Ignition!
let cancellable = publisher.sink(receiveCompletion: { _ in }, receiveValue: { statistics in
print("fresh statistics: \(statistics)")
})
// Expect change notification
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
try! dbPool.write { db in
try db.execute(sql: """
INSERT INTO tweet DEFAULT VALUES;
INSERT INTO tweet DEFAULT VALUES;
""")
}
print("Will we count two tweets?")
} Output (the initial duplicate value is due to #937):
I tried adding columns, giving Yet we may be onto something. The Let's figure out! Can you please give more information about the table for tweets, and the way you define the |
OK @steipete, I have tests that fail due to table name case-sensitivity. A workaround is to make sure GRDB only builds SQL requests that use the exact same database table name as in the database file: struct Tweet {
#warning("TODO: remove when GRDB#954 is fixed")
static let databaseTableName = "TwEeT"
} This should do the trick. Thanks for the report! A fix will ship shortly! |
Dang you are fast! Do you recommend to keep all table names lowercased? I current use CamelCase for the tables. |
SQLite is case-insensitive: GRDB is supposed to accept all casing flavors and variations, and this is why this issue reveals a bug, and that the test suite is incomplete :-) The documentation makes a recommendation, which is to name database tables just as a
|
Thanks for the explanation! I've changed my tables to your default, but good to see this fixed! |
Fixed in v5.7.4 |
What did you do?
I use this code to get data via a value observer:
Is this a known limitation?
When I call
print()
the difference is:vs
What did you expect to happen?
The observer should fire as new rows are added.
What happened instead?
The observer isn't detecting changes.
Environment
GRDB 5.6, iOS 14.5, Xcode 12.5b3, macOS Big Sur 11.3b6
The text was updated successfully, but these errors were encountered: