Group Group Group Group Group Group Group Group Group

Using @FetchRequest

I’ve read the whole book and i loved it. Got stuck in a few parts but in general i’m happy with the content.
I just felt a little underwhelmed on the specifics for @FetchRequest so i was hoping someone can help me out with a couple of questions.

  1. Is it possible to use it inside my MVVM class, right now i’m using the fetch request inside my view that presents let’s say “items” but i want to manage that fetch request inside my MVVM class that does the url request for those items on the backend, is there a protocol or a way to make this work more “sexy” ?

  2. I think the way @Fetchrequest works is really amazing, simple and direct, a lot of black box but i love it. Just found a problem in my current project, i have a view that presents core data items, but i have to present different predicates so for now i’m using just several fetch requests with a different predicate for each need it “filtering” for the presentation. Is there a compound or a better practice to manage several fetch requests and also how do i compound those.

Any help or directions would be really appreciate it, thanks!

Ok so just in case someone is looking for the answer for my SECOND question i found an answer here:

Basically create and extension for your NSManagedObject that returns an NSFetchRequest and use that on @FetchRequest as value, inside your extension where you return your NSFetchRequest either use optional parameters or create overloading to allow limit/sort/predicates:

static func allItems(limit:Int = 0, predicate:NSPredicate? = nil, sort:[NSSortDescriptor] = []) ->
NSFetchRequest {
let request: NSFetchRequest = Item.fetchRequest()
if let predicate = predicate {
request.predicate = predicate
}
// :sparkle: The @FetchRequest property wrapper in the ContentView requires a sort descriptor
request.sortDescriptors = sort
request.fetchLimit = limit
return request
}

@wilsonilo Do you still have issues with this?

Still haven’t found an approach that uses @FetchRequest inside an MVVM file, but i’m using EnvironmentObject to work around some of my needs, i guess this can be set as resolved for now. Thanks

Have you looked @ this?

MVVM with Combine Tutorial for iOS