'Swift and Core Data: calling 'await perform' inside another 'await perform'

I am using the iOS15 Swift + Core Data code for async/await, and while using private queue concurrency, I am trying to call a await perform block inside another await perform block:

await privateMainContext.perform({
    
    var displayName: String? = nil
    await self.managedObjectContext?.perform {
        displayName = self.displayName
    }
    // ... use displayName for privateContext object
})

But I get an error with the compiler that says:

Cannot pass function of type '() async -> ()' to parameter expecting synchronous function type

If I remove the await from the inner perform call, it works OK, but doesn't that mean the self.managedObjectContext will call that in a block that might not execute before I need to use the displayName property? With old Objective-C code, I would call performBlockAndWait within another context's performBlockAndWait block and it worked fine.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source