# Signature of NSManagedObjectContext perform

**URL:** <https://forums.swift.org/t/signature-of-nsmanagedobjectcontext-perform/76576>\
**Category:** Using Swift\
**Tags:** concurrency, coredata\
**Created:** [December 12, 2024, 6:59pm UTC](https://forums.swift.org/t/signature-of-nsmanagedobjectcontext-perform/76576 "2024-12-12T18:59:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![premdas26](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@premdas26](https://forums.swift.org/u/premdas26)\
**Post date:** [December 12, 2024, 6:59pm UTC](https://forums.swift.org/t/signature-of-nsmanagedobjectcontext-perform/76576/1 "2024-12-12T18:59:21Z")

</div>

Hi all, I am running into an oddity with the NSManagedObjectContext `perform` method and want to see if anyone else has run into this/agrees with my thoughts. Or perhaps on the other hand, you can explain why I'm mistaken 🙂

I would like to do something like this:

```swift
var result: Entity?

try await self.context.perform {
    let decoder = JSONDecoder()
    decoder.userInfo[.managedObjectContext] = context
    result = try decoder.decode(Entity, from: Data)
}

return result

```

However, the block passed to `perform` is escaping, so I cannot do this. What I don't understand is why `perform` is both async and has an escaping closure. It seems to me like it should either be async and not escaping, allowing me to use it as I'm attempting to do above or should not be async and take an escaping closure with the ability to bridge it to an async context with a continuation. Can someone help shed some light on what I'm missing?

I know I could use performAndWait instead to achieve this result, but I would prefer if I didn't have to block the calling thread.

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [December 12, 2024, 7:28pm UTC](https://forums.swift.org/t/signature-of-nsmanagedobjectcontext-perform/76576/2 "2024-12-12T19:28:57Z")

</div>

I haven't used Core Data in a while, but [the async variant of `NSManagedObjectContext.perform`](https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/3802018-perform) returns the closure's return value to its own caller, so you should be able to write this:

```swift
let result: Entity = try await self.context.perform {
    let decoder = JSONDecoder()
    decoder.userInfo[.managedObjectContext] = context
    return try decoder.decode(Entity.self, from: data)
}

```

(Note: I removed the optionalness of `result: Entity?` because it made more sense to me this way. And I made some other changes in the `decoder.decode` line: `Entity.self`; `data` instead of `Data`.)

---

<div class="post-metadata">

**Author:** ![premdas26](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@premdas26](https://forums.swift.org/u/premdas26)\
**Post date:** [December 12, 2024, 8:30pm UTC](https://forums.swift.org/t/signature-of-nsmanagedobjectcontext-perform/76576/3 "2024-12-12T20:30:56Z")

</div>

Ah, I didn't realize you could return a value from the closure that way. This is just what I needed, thank you!

And yes, thank you for the updates. I was translating this to be more generic from my code and made some mistakes which your updates corrected 🙂

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [December 12, 2024, 8:38pm UTC](https://forums.swift.org/t/signature-of-nsmanagedobjectcontext-perform/76576/4 "2024-12-12T20:38:08Z")

</div>

Yeah, it's nice API design. For what it's worth, many standard library APIs use the same pattern. For example:

- [`withTaskGroup`](https://developer.apple.com/documentation/swift/withtaskgroup(of:returning:isolation:body:))
- [`Mutex.withLock`](https://developer.apple.com/documentation/synchronization/mutex/withlock(_:))
- (and many more `with…`-style APIs)
