# Using @FetchRequest

**URL:** <https://forums.kodeco.com/t/using-fetchrequest/100735>\
**Category:** Combine: Asynchronous Programming with Swift\
**Created:** [March 6, 2020, 12:03am UTC](https://forums.kodeco.com/t/using-fetchrequest/100735 "2020-03-06T00:03:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilsonilo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/wilsonilo/32/94894_2.png) [@wilsonilo](https://forums.kodeco.com/u/wilsonilo)\
**Post date:** [March 6, 2020, 12:03am UTC](https://forums.kodeco.com/t/using-fetchrequest/100735/1 "2020-03-06T00:03:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![wilsonilo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/wilsonilo/32/94894_2.png) [@wilsonilo](https://forums.kodeco.com/u/wilsonilo)\
**Post date:** [March 9, 2020, 7:25pm UTC](https://forums.kodeco.com/t/using-fetchrequest/100735/2 "2020-03-09T19:25:04Z")

</div>

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

> **[How to Pass NSManagedObjectContext to a SwiftUI View](https://www.andrewcbancroft.com/blog/ios-development/data-persistence/passing-nsmanagedobjectcontext-to-swiftui-view/#first-things-first)**
>
> Breaks down how to pass an NSManagedObjectContext instance to your SwiftUI Views.

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  
}  
// ❇ The @FetchRequest property wrapper in the ContentView requires a sort descriptor  
request.sortDescriptors = sort  
request.fetchLimit = limit  
return request  
}

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 4, 2020, 11:44pm UTC](https://forums.kodeco.com/t/using-fetchrequest/100735/3 "2020-04-04T23:44:22Z")

</div>

@wilsonilo Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![wilsonilo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/wilsonilo/32/94894_2.png) [@wilsonilo](https://forums.kodeco.com/u/wilsonilo)\
**Post date:** [April 6, 2020, 3:22pm UTC](https://forums.kodeco.com/t/using-fetchrequest/100735/4 "2020-04-06T15:22:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![billm](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/billm/32/81772_2.png) [@billm](https://forums.kodeco.com/u/billm)\
**Post date:** [April 6, 2020, 3:59pm UTC](https://forums.kodeco.com/t/using-fetchrequest/100735/5 "2020-04-06T15:59:50Z")

</div>

Have you looked @ this?

[MVVM with Combine Tutorial for iOS](https://www.raywenderlich.com/4161005-mvvm-with-combine-tutorial-for-ios)
