implementing Core Data Faulting in macOS 13.6 Causing Memory Overhead
I'm getting frustrated with I'm experiencing important memory overhead when using Core Data in my macOS 13.6 app. Despite implementing faulting, it seems like Core Data is loading entire objects into memory instead of just faults. For context, I'm fetching a large dataset of 'User' entities, which have several relationships to 'Post' entities. When I run the following fetch request: ```swift let fetchRequest: NSFetchRequest<User> = User.fetchRequest() fetchRequest.predicate = NSPredicate(format: "active == true") fetchRequest.fetchBatchSize = 20 let users = try context.fetch(fetchRequest) ``` I expect to only load the 'User' entities, but when I inspect `users`, I notice that all related 'Post' entities are also being loaded into memory, which is causing my app to consume a important amount of RAM, especially when there are thousands of users. I have tried setting the `relationshipKeyPathsForPrefetching` property on the fetch request to limit what gets loaded, but it doesn't seem to have any effect. Here’s the code I used: ```swift fetchRequest.relationshipKeyPathsForPrefetching = ["posts"] ``` I also verified that the 'User' entity’s relationship to 'Post' is set as a to-many relationship with 'delete cascading' configured. To troubleshoot, I enabled Core Data's SQL debugging log by adding `-com.apple.CoreData.SQLDebug 1` to the launch arguments, and I found that the SQL generated doesn't seem to prefetch anything unnecessarily, indicating that the scenario might be at the application level. Are there any best practices to ensure that Core Data only faults objects and doesn’t load them into memory when they aren’t needed? Any insights or suggestions would be greatly appreciated! I'm coming from a different tech stack and learning Swift.