Fetching on a background thread #224

Closed
opened 2025-12-29 15:26:57 +01:00 by adam · 1 comment
Owner

Originally created by @Guferos on GitHub (Jul 31, 2018).

It might sound trivial but I cannot find a good way to do it. I need to pass a Core Data object to a background thread where I will be only performing read operations with that object.

\\ Main thread
let myCDObject = .....

\\ My background queue
actionsQueue.sync {
    // fetch myCDObject here and perform read operations safely
}

The only way I found to do it is:

\\ Main thread
let myCDObject = .....

\\ My background queue
actionsQueue.sync {
            CoreStore.perform(asynchronous: { (transaction) in
                guard let myCDObject = transaction.fetchExisting(myCDObject) else {
                    return
                }
                ...
                \\ my code where I only perform read operations on the myCDObject
                ...
            }, completion: {_ in})
  }

However it feels wrong as my code will be performed on the CoreStore queue instead of mine.
Is there a way to create a transaction inside the actionsQueue and fetch the myCDObject without wrapping it into perform asynchronous block?

Originally created by @Guferos on GitHub (Jul 31, 2018). It might sound trivial but I cannot find a good way to do it. I need to pass a Core Data object to a background thread where I will be only performing read operations with that object. ``` \\ Main thread let myCDObject = ..... \\ My background queue actionsQueue.sync { // fetch myCDObject here and perform read operations safely } ``` The only way I found to do it is: ``` \\ Main thread let myCDObject = ..... \\ My background queue actionsQueue.sync { CoreStore.perform(asynchronous: { (transaction) in guard let myCDObject = transaction.fetchExisting(myCDObject) else { return } ... \\ my code where I only perform read operations on the myCDObject ... }, completion: {_ in}) } ``` However it feels wrong as my code will be performed on the CoreStore queue instead of mine. Is there a way to create a transaction inside the actionsQueue and fetch the myCDObject without wrapping it into perform asynchronous block?
adam closed this issue 2025-12-29 15:26:57 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jul 31, 2018):

You can use CoreStore.perform(synchronous:) if your queueing mechanism isn't that complex, but you may encounter deadlocks otherwise. If you are sure to be just reading from the object, you can use CoreStore.beginUnsafe() to create a transaction that acts as a background, read-only, temporary transaction. From that you can just fetch your object within your queue.

@JohnEstropia commented on GitHub (Jul 31, 2018): You can use `CoreStore.perform(synchronous:)` if your queueing mechanism isn't that complex, but you may encounter deadlocks otherwise. If you are sure to be just reading from the object, you can use `CoreStore.beginUnsafe()` to create a transaction that acts as a background, read-only, temporary transaction. From that you can just fetch your object within your queue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#224