I have a bug I've come across since I've upgraded Xcode (16.0) and macOS (15.0 Sequoia) and figured I'd create a minimal viable example in case someone else came across this and help. I've noticed this in both the macOS and iOS version of my app when I run it.
Essentially I have an area of my code that calls a class that has a step 1 and 2 process. The first step uses async let _ = await methodCall(...)
to create some SwiftData
items while the second step uses a TaskGroup
to go through the created elements, checks if an image is needed, and syncs it. Before this worked great as the first part finished and saved before the second part happened. Now it doesn't see the save and thus doesn't see the insertions/edits/etc in the first part so the second part just isn't done properly. Those step one changes are set and shown in the UI so, in this case, if I run it again the second part works just fine on those previous items while any newly created items are skipped.
I came across this issue when step one handled 74 inserts as each one didn't contain an image attached to it. When I switched the async let _ = await methodCall(...)
to a TaskGroup
, hoping that would wait better and work properly, I had the same issue but now only 10 to 30 items were created from the first step.
Minimal Viable Example: to reproduce something similar
In my minimal viable sample I simplified it way down so you can't run it twice and it's limited it creating 15 subitems. That said, I've hooked it up with both an async let _ = await methodCall(...)
dubbed AL
and a TaskGroup
dubbed TG
. With both types my second process (incrementing the number associated with the subissue/subitem) isn't run as it doesn't see the subitem as having been created and, when run with TaskGroup
, only 12 to 15 items are created rather that the always 15 of async let
.
Code shared here: https://gist.github.com/SimplyKyra/aeee2d43689d907d7a66805ce4bbf072
And this gives a macOS view of showing each time the button is pressed the sub issues created never increment to 1 while, when using TaskGroup, 15 isn't guaranteed to be created and remembered.
I'm essentially wondering if anyone else has this issue and if so have you figured out how to solve it? Thanks
Hi @SimplyKyra , this is really and interesting approach with SwiftData (wrapper on CoreData framework) and concurrency programs.
The issue could be in the creation of the sub issue inside the taskGroup.
I think that the problem could be in a concurrency access on ModelContext inside all single task groups. Putting a @MainActor on this method
@MainActor
internal final func createSubIssue(in container: ModelContainer, issueID: PersistentIdentifier, name: Int) async -> String?
resolve the concurrency problem on ModelContext, but I'm not sure could be the right approach, I attached a screenshot.
On CoreData framework there is the concept of background managed context (model Context is the SwiftUI name of this object).
Think you should follow this approach on SwiftUI
https://developer.apple.com/documentation/coredata/using_core_data_in_the_background
Bye Rob