mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
How to have performant fetches from a data stack without locking the main thread, but still allow reads concurrently with writes #405
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jimbengtsson92 on GitHub (Mar 7, 2023).
In our app all of our writes basically look like this:
and our reads like this:
With this setup we can achieve concurrent read and writes, but when performing a read this is locking the main thread. If I do not perform the fetch on the main thread I get a fatalError saying "Attempted to fetch from a 'CoreStore.DataStack' outside the main thread." error from CoreStore.
One way I have found we can do it to perform reads without locking the main thread is to use a transaction for these as well, like this:
With this approach I cannot however perform reads concurrently with writes, since they are both run on the same background serial queue that is created from the transaction. This is making the performance way worse in the cases when we are trying to do writes at around the same time as we do reads.
Do you, or anyone else, have any advice on how to do create fetches that do not lock the main thread, but can still run concurrently with writes?
@JohnEstropia commented on GitHub (Mar 7, 2023):
The fetches are fine, but the error message indicates you are accessing the fetched objects' properties outside the main thread
@jimbengtsson92 commented on GitHub (Mar 8, 2023):
@JohnEstropia
The error comes from this method in
DataStack+Querying.swift:So it looks like it is the actual fetch that is the problem.
An example of a complete fetch where this error happens is:
@JohnEstropia commented on GitHub (Mar 8, 2023):
@jimbengtsson92 I see, then it's exactly as the method says: fetches from
DataStackinstances should come from the main thread. If you want to fetch things asynchronously, you can write something like:Just be careful not to access the relationship objects directly from the snapshots, as those need to be fetched again from either a
DataStackor a transaction.Then when you need to edit the objects for these snapshots inside transactions, convert them back into the transaction instance by doing
snapshot.asEditable(in: transaction)