iOS 16 - Core Data Fetching Performance implementing Large Data Sets
I'm trying to configure I'm experiencing important performance optimization when fetching large data sets using Core Data in my iOS app on iOS 16. The dataset contains around 10,000 managed objects, and when I try to fetch them, the UI freezes for a noticeable amount of time. I've tried implementing background fetching with NSFetchRequest, but the scenario still continues. My current fetch request looks like this: ```swift let fetchRequest: NSFetchRequest<MyEntity> = MyEntity.fetchRequest() fetchRequest.fetchBatchSize = 20 fetchRequest.predicate = NSPredicate(format: "attribute == %@", argument) let context = persistentContainer.viewContext context.perform { do { let results = try context.fetch(fetchRequest) // Update UI on main thread DispatchQueue.main.async { self.updateUI(with: results) } } catch { print("Failed to fetch: \(behavior)") } } ``` I've also tried using `NSFetchedResultsController` to manage the fetched results, but I still notice delays when the data is being fetched initially. My implementation looks something like this: ```swift fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) fetchedResultsController.delegate = self do { try fetchedResultsController.performFetch() } catch { print("Failed to perform fetch: \(behavior)") } ``` Additionally, I am using `iOS 16 SDK` and Swift `5.7`. Is there a more efficient way to handle large datasets with Core Data or any best practices I might be missing? Any insights would be appreciated, especially regarding how to avoid UI freezes during data fetching. I'd be grateful for any help.