Originally created by @colinmorelli on GitHub (Apr 28, 2016).
Back again with another issue/question.
I see this has been asked before #9 and #33 - so I apologize in advance if you're already against this idea. However, I'm looking to create a transaction and get access to the internalContext that drives it. While I can use beginUnsafe(), it forces my hand to use a queue with QOS of user initiated, which this is not.
My use case is a background mapping operation. I use a library to map JSON into CoreData entities, and it uses a MOC to insert/update entities based on the JSON data. I'd be completely fine using beginUnsafe() and manually ensuring thread safety myself for this case, if we could just expose the option to choose which queue to use (if you want, I think user initiated makes sense by default).
Originally created by @colinmorelli on GitHub (Apr 28, 2016).
Back again with another issue/question.
I see this has been asked before #9 and #33 - so I apologize in advance if you're already against this idea. However, I'm looking to create a transaction and get access to the internalContext that drives it. While I can use `beginUnsafe()`, it forces my hand to use a queue with QOS of user initiated, which this is not.
My use case is a background mapping operation. I use a library to map JSON into CoreData entities, and it uses a MOC to insert/update entities based on the JSON data. I'd be completely fine using `beginUnsafe()` and manually ensuring thread safety myself for this case, if we could just expose the option to choose which queue to use (if you want, I think user initiated makes sense by default).
adam
added the question label 2025-12-29 18:22:00 +01:00
So you can actually just call commitAndWait() freely from any queue you wish.
GCDQueue.UserInitiated.async{lettransaction=CoreStore.beginUnsafe()letcontext=transaction.internalContext()// ... do some edits ...transaction.commitAndWait()}
@JohnEstropia commented on GitHub (Apr 28, 2016):
`UnsafeDataTransaction` doesn't actually use the queue it gets from its `init(...)` method. You can see for yourself in `UnsafeDataTransaction.swift`:
``` swift
public func commit(completion: (result: SaveResult) -> Void) {
self.context.saveAsynchronouslyWithCompletion { (result) -> Void in
self.result = result
completion(result: result)
}
}
public func commitAndWait() -> SaveResult {
let result = self.context.saveSynchronously()
self.result = result
return result
}
```
So you can actually just call `commitAndWait()` freely from any queue you wish.
``` swift
GCDQueue.UserInitiated.async {
let transaction = CoreStore.beginUnsafe()
let context = transaction.internalContext()
// ... do some edits ...
transaction.commitAndWait()
}
```
No problem! I should remove the queue in that init code though so people don't get confused.
If you have other questions feel free to ask anytime :)
@JohnEstropia commented on GitHub (Apr 28, 2016):
No problem! I should remove the queue in that init code though so people don't get confused.
If you have other questions feel free to ask anytime :)
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 @colinmorelli on GitHub (Apr 28, 2016).
Back again with another issue/question.
I see this has been asked before #9 and #33 - so I apologize in advance if you're already against this idea. However, I'm looking to create a transaction and get access to the internalContext that drives it. While I can use
beginUnsafe(), it forces my hand to use a queue with QOS of user initiated, which this is not.My use case is a background mapping operation. I use a library to map JSON into CoreData entities, and it uses a MOC to insert/update entities based on the JSON data. I'd be completely fine using
beginUnsafe()and manually ensuring thread safety myself for this case, if we could just expose the option to choose which queue to use (if you want, I think user initiated makes sense by default).@JohnEstropia commented on GitHub (Apr 28, 2016):
UnsafeDataTransactiondoesn't actually use the queue it gets from itsinit(...)method. You can see for yourself inUnsafeDataTransaction.swift:So you can actually just call
commitAndWait()freely from any queue you wish.@colinmorelli commented on GitHub (Apr 28, 2016):
Yep, now I feel really dumb. Should have looked deeper into it. Sorry about that.
@JohnEstropia commented on GitHub (Apr 28, 2016):
No problem! I should remove the queue in that init code though so people don't get confused.
If you have other questions feel free to ask anytime :)