mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-12 04:10:36 +01:00
Invoke DataStack.fetchExisting from a background thread #143
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 @ujamshy on GitHub (Jun 7, 2017).
i need to access a managed object received from ListMonitor and process it on a background thread.
General scheme is:
onDidInsertObject { object in // this is called on the main queue
localQueue.async { // doing work on a background queue
let localObject = self.dataStack.fetchExisting(object.objectId)
// access localObject properties (no modifications)
}
}
This will randomly cause crash when accessing the localObject properties.
Can we invoke DataStack.fetchExisting on a background thread, and if not what is the suggested scheme to avoid processing on the main thread.
thanks.
@JohnEstropia commented on GitHub (Jun 7, 2017):
Use a temporary
UnsafeDataTransactionin your background thread:Make sure
transactionisn't released while you are using your objects.You can use
withExtendedLifetime(...)for that:@ujamshy commented on GitHub (Jun 7, 2017):
Thanks for your answer and for your awesome framework.
Just to make sure:
DataStack.fetchExisting should be always called on main queue, is it correct?
@JohnEstropia commented on GitHub (Jun 7, 2017):
@ujamshy Yes,
DataStack's methods are meant to be used in the main thread (except for the methods that create transactions). Basically, you just need to remember thatDataStack.fetchExisting(_:): main queue onlyAsynchronousDataTransaction.fetchExisting(_:): within the transaction's queue onlySynchronousDataTransaction.fetchExisting(_:): within the transaction's queue onlyUnsafeDataTransaction.fetchExisting(_:): any queue