Questions tagged [nsfetchrequest]

On macOS 10.4+ and iOS 3.0+, an instance of NSFetchRequest describes the search criteria used to retrieve data from a persistent store. It is a class in the Apple Core Data Framework.

From the NSFetchRequest documentation:

An instance of NSFetchRequest collects the criteria needed to select and optionally to sort a group of managed objects held in a persistent store. (See NSPersistentStore and NSManagedObject.) A fetch request must contain an entity description (an instance of NSEntityDescription) or an entity name that specifies which entity to search. It frequently also contains

  • A predicate (an instance of NSPredicate) that specifies which properties to filter by and the constraints on selection, for example, “last name begins with a ‘J’”. If you don’t specify a predicate, then all instances of the specified entity are selected (subject to other constraints; see executeFetchRequest:error: for full details).

  • An array of sort descriptors (instances of NSSortDescriptor) that specify how the returned objects should be ordered, for example, by last name then by first name.

Questions related to this can also be tagged with .

NSFetchRequest objects can be used with the method executeFetchRequest:error:, defined by NSManagedObjectContext.

Sample syntax:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"
                inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
1115 questions
9
votes
2 answers

How to use a Fetch request from xcdatamodeld?

I'm noob with Core Data and I can't find the help I need on the Apple developer website. I defined a fetch request in my .xcdatamodeld file but now I can't find how to use it? This is the definition of the fetch request : I supose that the fetch…
Maxime
  • 1,332
  • 1
  • 15
  • 43
9
votes
1 answer

"Generic parameter 'ResultType' could not be inferred" while use NSFetchRequest() with swift 3.0

While I convert my project to swift 3.0 , I found that error parameter 'ResultType' could not be inferred My code like this : let fetchRequest = NSFetchRequest(entityName: "Book") I use this code in my project before,and now it appears error.How…
user6847532
9
votes
1 answer

Core Data - NSPredicate to filter to-many relationship

I have 2 entities, Task and List. Each task has a to-one relationship to a List object called "list", and there is an inverse relationship with List, which has a to-many relationship with Task called "tasks". I'm trying to use a fetch request with…
indragie
  • 18,002
  • 16
  • 95
  • 164
9
votes
1 answer

iOS: NSPredicate using NSDate to compare

I have an NSDate attribute called startDate stored in the persistence store in the following format (picture below). 426174354 = July 04, 2014 I need to create (3) NSFetchRequest using predicates. For startDate: fetchRequest1 using predicate(s)…
user1107173
  • 10,334
  • 16
  • 72
  • 117
9
votes
2 answers

How can I know the class type of an abstract entity in a NSPredicate?

Using core data I'd like to fetch some data. My model uses some abstract entities, see attached picture, where QuantifiedIngredient is an abstract class. I'd like to fetch Ingredient entities that have at least one RecipeQuantifiedIngredients, but…
Ricardo
  • 2,831
  • 4
  • 29
  • 42
8
votes
2 answers

Core Data Performance: NSPredicate comparing objects

If my Author NSManagedObject model has a authorID attribute (determined by the server), will an NSFetchRequest perform better if the NSPredicate filters by authorID rather than the complete Author object? Let's say I'm fetching all Book…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
8
votes
1 answer

How to automatically reflect CoreData+iCloud Changes in SwiftUI view?

So...I have an app with StoreData and iCloud enabled. My data is syncing between the devices but I don't ge the behaviour I want when it comes to reflecting the changes in the UI. The behaviour I want: When a user has my app open on two devices (A…
iSebbeYT
  • 1,441
  • 2
  • 18
  • 29
8
votes
0 answers

SwiftUI FetchRequest and onReceive cause infinite loop

I have a @FetchRequest for NSManagedObject in swiftUI. I’m trying to change the title of the first item in core data when the toggle is used, by calling onReceive. This does not work if the fetched results are used in MainVC. To demonstrate this,…
Richard Witherspoon
  • 4,082
  • 3
  • 17
  • 33
8
votes
2 answers

NSPredicate Returns No Results with Fetch Request, Works with Array Filtering

My situation is simple: I have some records in my core data store. One of their attributes is a string called "localId". There's a point where I'd like to find the record with a particular localId value. The obvious way to do this is with an…
CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
8
votes
1 answer

Swift Fetch Request Returning Empty Item In Results

I have a project where I need to do a fetch request that gets the most recent 'updated' date of a Core Data entity. When I actually examine the results returned by my query however I am seeing some rather strange behaviour. In addition to the…
pbuchheit
  • 1,371
  • 1
  • 20
  • 47
8
votes
2 answers

Variable Substitution with FetchRequests stored in a CoreData Model

I've always created my NSFetchRequests entirely in-code. Now I'm looking at the Xcode GUI for building a fetch request and storing it in the model. I'm following an example from the Xcode Documentation. I added a Fetch Request to my model, and the…
Woodster
  • 3,451
  • 3
  • 17
  • 19
8
votes
2 answers

Core Data: trying to find minimum date for an attribute in an entity

I'm trying to find the oldest date in a particular attribute in Core Data. I've found an example in the Core Data Programming Guide that purports to do exactly that, but keep getting an unrecognized selected error when I run it. My code (with only…
AndrewO
  • 1,590
  • 1
  • 17
  • 24
8
votes
0 answers

Batch faulting in a to-many relationship for a collection of objects

Scenario: Let's say I have an entity called Author that has a to-many relationship called books to the Book entity (inverse relationship author). If I have an existing collection of Author objects, I want to fault in the books relationship for all…
indragie
  • 18,002
  • 16
  • 95
  • 164
7
votes
2 answers

Core Data NSPredicate filter by entity class?

How would I create an NSPredicate to filter by entity of class Contact? The solution to NSPredicate check for kind of object class crashes: [NSPredicate predicateWithFormat:@"person.class == %@", [Contact class]]; *** Terminating app due to…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
7
votes
1 answer

Sort by entity name in NSFetchRequest

I have a fetched results controller that should display all items of a certain entity that has a number of subentities. The sections in the fetched results controller should be based on the entity name, i.e which subentity an item belongs to.…
mrueg
  • 8,185
  • 4
  • 44
  • 66
1 2
3
74 75