Exposing NSManagedObjectContext #54

Closed
opened 2025-12-29 15:23:08 +01:00 by adam · 3 comments
Owner

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 15:23:08 +01:00
adam closed this issue 2025-12-29 15:23:08 +01:00
Author
Owner

@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:

    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.

GCDQueue.UserInitiated.async {

    let transaction = CoreStore.beginUnsafe()
    let context = 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() } ```
Author
Owner

@colinmorelli commented on GitHub (Apr 28, 2016):

Yep, now I feel really dumb. Should have looked deeper into it. Sorry about that.

@colinmorelli commented on GitHub (Apr 28, 2016): Yep, now I feel really dumb. Should have looked deeper into it. Sorry about that.
Author
Owner

@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 :)

@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 :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#54