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
5
votes
2 answers

efficiently display 100,000 items using Core Data

I am using a NSFetchResultsController to display 100,000 + records in a UITableView. This works but it is SLOW, especially on an iPad 1. It can take 7 seconds to load which is torture for my users. I'd also like to be able to use sections but this…
Slee
  • 27,498
  • 52
  • 145
  • 243
5
votes
2 answers

CoreData NSFetchRequest returns results but section number of objects returns 0

I am currently trying to write my first CoreData project and am getting very confused. I am using the following code to setup a fetched results controller: - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController…
5
votes
3 answers

how we can search all records using NSPredicate rather than setting fetchController nil?

I am showing all the records in UITableView easily by using: if (_fetchedResultsController != nil) { return _fetchedResultsController; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity =…
5
votes
1 answer

NSPredicate 'The left hand side for an ALL or ANY operator must be either an NSArray or NSSet'

Not totally sure why this isn't working now, i thought it had been working previously. Does anyone see an issue with this FetchRequest construction? - (NSArray *)entriesForDate:(NSDate *)date { NSFetchRequest *request = [[NSFetchRequest…
Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90
5
votes
3 answers

Is there anything bad about reusing an NSFetchRequest for several different fetches with Core Data?

My question: Is there anything bad about reusing an NSFetchRequest for several different fetches with Core Data? Example code: NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *logEntity = [NSEntityDescription…
Pieter
  • 1,751
  • 3
  • 30
  • 65
5
votes
3 answers

NSSortdescriptor ineffective on fetch result from NSManagedContext

I'm trying to sort my NSFetchRequest result using a NSSortdescriptor using a key pointing to a NSDate value. My fetch results come out totally random for no clear reason. The NSManagedObjectContext I'm using is updated with a save from a nested…
poof86
  • 98
  • 2
  • 8
4
votes
2 answers

Performing multiplication (aggregation) with CoreData: how to?

Following a fantastic tutorial by Jeff Lamarche, I'm trying to aggregate data for a specific subclass of NSManagedObject. This is the scenario. I created a class named Product that extends NSManagedObject class. Product class has three properties…
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
4
votes
1 answer

"Whole word" search in a NSString through NSPredicate

I would like to search if in the attribute description (an NSString instance) there is a given word. I tried with this predicate: [NSPredicate predicateWithFormat:@"description CONTAINS[cd] %@", theWord]; It works, but it finds also sub-words. For…
Dev
  • 7,027
  • 6
  • 37
  • 65
4
votes
3 answers

NSFetchRequest not catching objects that have a changed property

I have run into a weird problem with CoreData on MacOsX 10.6 using an SQL store. I have an NSManagedObject subclass called Family with attribute name and a relationship personList connected to another NSManagedObject subclass called Person with…
Jean-Marc
  • 61
  • 3
4
votes
1 answer

Core Data: Sort by Relationship's Attribute

I'm building an open-source clone of iPhone's native Messages app called AcaniChat on GitHub. I have a Conversation entity and a Message entity with a sentDate attribute. Each Conversation can have many Messages. How do I fetch Conversations sorted…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
4
votes
1 answer

ios NSFetchRequest, order child objects

Hi would like to know how to specify a FetchRequest where i can order the objects in a relationship. | Parent | | Child | | - name |------->| - name | | - position | | - position | For…
user346443
  • 4,672
  • 15
  • 57
  • 80
4
votes
1 answer

How to unset NSFetchRequest's fetchLimit?

The documentation for Cocoa's NSFetchRequest fetchLimit property says: The fetch limit specifies the maximum number of objects that a request should return when executed. If you set a fetch limit, the framework makes a best effort to improve…
Josh Sherick
  • 2,161
  • 3
  • 20
  • 37
4
votes
1 answer

Core Data: Fetch all entities in a to-many-relationship of a particular object?

in my iPhone application I am using simple Core Data Model with two entities (Item and Property): Item name properties Property name value item Item has one attribute (name) and one one-to-many-relationship (properties). Its inverse relationship…
4
votes
6 answers

-[NSCFNumber count]: unrecognized selector

I've got some Core Data code that follows Apple's sample code precisely (the Fetching Attribute Values that Satisfy a Given Function example). I'm using it to get the max value of a field, so I can then increment it when I insert the next object of…
Dov
  • 15,530
  • 13
  • 76
  • 177
4
votes
4 answers

How to insert a new Object into an Entity and set the relationship of that object with an existing object of another entity?

My question can be applied to any type of relationship but in my case I have a one to many. Let's say I have an Entity Person which has 3 objects: Brian Peter Scott And each person can have many Jobs. So I have a Job entity that is currently empty.…
Fidel
  • 1,173
  • 11
  • 21