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?
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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.
The only way I found to do it is:
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?
@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 useCoreStore.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.