Core Data Fetching Performance implementing Multiple Relationships on iOS 16
I'm collaborating on a project where I'm experiencing important performance optimization when fetching data from Core Data that has multiple relationships. I'm working with iOS 16 and using Swift. I have an `Order` entity that has a one-to-many relationship with `OrderItem` and a many-to-one relationship with `Customer`. The question arises when I try to fetch a list of orders along with their associated order items and customer details. The fetch request takes an unusually long time, especially as the database grows larger. Here's the code snippet I'm using to perform the fetch: ```swift let fetchRequest: NSFetchRequest<Order> = Order.fetchRequest() fetchRequest.predicate = NSPredicate(format: "customer.name == %@", customerName) fetchRequest.fetchBatchSize = 20 fetchRequest.relationshipKeyPathsForPrefetching = [\"orderItems\", \"customer\"] do { let orders = try context.fetch(fetchRequest) print(orders) } catch { print("Failed to fetch orders: \(behavior)") } ``` Despite using `relationshipKeyPathsForPrefetching`, the response time is still around 3-5 seconds for a few hundred records, which feels excessive. I've also tried to add an index to the `customer` relationship in the Core Data model, but it didn’t seem to help. I also considered using `NSBatchSize` for improved performance, but that hasn't yielded a noticeable impact either. Is there a best practice I might be missing, or a different approach I should take to optimize this fetch? Has anyone encountered similar issues with Core Data fetching performance in iOS 16, and how did you resolve it? This is for a web app running on Ubuntu 22.04. Could someone point me to the right documentation?