unsafeFetchRecordsWithSQL & fetch - possible race condition?
See original GitHub issueAlright, I’ve spent a fairly reasonable amount of time attempting to debug this including putting log statements in the node_modules
source of watermelondb
.
In a nutshell the symptom of the issue is that I’m getting the classic [Diagnostic error: Record ID foos#12345 was sent over the bridge, but it's not cached
error message. It feels as though there might be some sort of race condition going on based on what I’m seeing, but I’ll let the experts be the judge of that. If I can provide any more information I’d be happy to. Here’s the breakdown:
I’m using unsafeFetchRecordsWithSQL
to fetch a particular record, and separately fetching a larger set of that records type with a traditional query/fetch. These two operations happen asynchronously in the context of one another. The minimum reproducible code that I’ve created is something to the effect of the following (please note: in reality the unsafe query is more complex, but the below appears to do the job of reproducing):
collection.unsafeFetchRecordsWithSQL(`
SELECT f.*
FROM foos AS f
WHERE f.id = '12345'
LIMIT 1
`)
.catch((err) => {
console.error('Error when executing unsafe fetch', err);
})
;
// `12345` will also be fetched by this query
collection.query()
.fetch()
.catch((err) => {
console.error('Error when executing safe collection fetch', err)
})
;
In this case, the safe fetch will throw the Diagnostic error: Record ID foos#12345 was sent over the bridge, but it's not cached]
error.
When logging, it appears that while the record cache has an ID in the map for 12345
it points to undefined
which I can only assume is the result of the fact that the unsafeFetchRecordsWithSQL
has begun, but hasn’t fully resolved.
Thoughts? Thanks in advance!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Yes I do, and so
fixing your own problems
is very much appreciated, especially if it comes with a pull request 😃 But happy to assist, I don’t want any observable race conditions in this project 😃unsafe raw: https://github.com/Nozbe/WatermelonDB/blob/master/src/Collection/index.js#L122-L124 fetch: https://github.com/Nozbe/WatermelonDB/blob/master/src/Query/index.js#L103-L105
the former is
async
, so it does work in multiple ticks… the latter works on callbacks, so if the adapter can call back synchrounsly, it will. I think what needs to happen is to refactor unsafeRawfetchsomethingsomething into callback style. That should work. A simple test case would be appreciated, to make sure no regressions can occur 😃@radex any chance you could take a look at the linked PR?