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.

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