SwiftUI Lag on Core Data/SwiftData Updates During Background Tasks

Hello, developers,

I'm experiencing UI lag in SwiftUI when updating Core Data or SwiftData entities in the background, noticeable during scrolling. This issue arises regardless of whether the updated entity is currently displayed. For instance, updating a "Song" entity with a new localURL from a background download and saving background context affects an unrelated "Artists" list view.

Scenario: The lag is most evident when completing background downloads and updating entity properties, causing a hitch in scrolling interactions when using SwiftUI @FetchRequest or @Query for displaying data.

Observations:

  • The issue occurs with both Core Data @FetchRequest and SwiftData @Query property wrappers.
  • It affects unrelated views, suggesting a broader issue with SwiftUI's handling of data updates during user interactions.

Seeking Community Insights:

  1. Has anyone faced similar issues with SwiftUI and background data updates?
  2. Any strategies for reducing this lag/hitch?
  3. Insights into SwiftUI's data update and view re-rendering process in this context?

For a practical example, Apple's project on loading and displaying a large data feed demonstrates this issue. Try tapping the refresh button while scrolling to observe the lag.

Appreciate any advice or solutions!

Best Regards, [Personal Information Edited by Moderator]

with the property wrappers its actually being done on the main thread. to actually do any fetching or saving you would need to do so on the background thread. just a heads up, when creating a modelactor you would want to create it on the background as a detached task from the parent view if not the calculations would still be done on the main thread.

I'm seeing this as a major issue as well, most of the time is taken up decoding my objects on the main thread.

I have the same issues in my app. I updating a lot of Models on a background thread with ModelActor (initialized with Task.detached) But when call save on the ModelActor modelContext this lag happens. Mostly recognizeable while scrolling or switching screens throught navigation.

I think its because calling save will also notify to update the mainContext, which is needed to update the UI through @Query. So it consumes a lot of main-thread time which makes the UI laggy.. I already implemented a solution to what until the scrollview stops, and calling save after that, but if the user starts scrolling again, the lag is recognizable again.I am experiencing the same issues in my app. I am updating several models on a background thread using ModelActor, which I initialized with Task.detached. However, when I call save on the ModelActor's model context, I notice a lag, particularly while scrolling or navigating between screens.

I believe this lag occurs because calling save also prompts an update to the main context, which is necessary for refreshing the UI for @Query. As a result, this process consumes a significant amount of time on the main thread, causing the UI to lag.

I've implemented a temporary solution by waiting until the scroll view stops before calling save. However, if the user begins scrolling again, the lag becomes noticeable once more.

I don't have any other solution besides avoiding @Query and using the background context only to fetch data..

SwiftUI Lag on Core Data/SwiftData Updates During Background Tasks
 
 
Q