Passing parameters to fetchRequest, SwiftUI - fetchOffset, fetchLimit

I'm trying to build a multi-page TabView with a .pageTabView style. Each page is a grid of objects, populated from Core Data. I'd like it to dynamically build, based on screen size and object count - so the number of objects on a page and the number of pages are dynamic. Also want to be able to filter on at least 1 attribute.

First, strategy - it seems that the 'recommended' process is to pull from Core Data on the fly. In other words, configure the page count and number of items per page, then call a FetchRequest to build out each page, so you're only handling the data once. The alternative is to do a single query, unwrap the fetchRequest, then filter and slice it. Then updates are separate new fetches.

Yes? Is there a better way to do this?

From what I've seen (Core Data debug on verbose), Core Data converts fetch requests to SQL and requires fetchOffset and fetchLimit to use literals. I haven't yet found a way to pass those in to the request. Apple's documentation is exhaustive on NSPredicate and NSSortDescriptors but barely mentions the others.

Since I don't have weeks to solve this, I've just built a bunch of static fetchRequests and use case switch to select the right one. There has to be a better way.

If you’re having to try and make modifications to fetchRequests, then you should probably be using fetchedresultscontroller. It’s kind of the purpose of it and handles your use-case.

Passing parameters to fetchRequest, SwiftUI - fetchOffset, fetchLimit
 
 
Q