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

NSFetchRequest as entityName gives error "use of undeclared type"

I'm using Core data with Swift 2.1 and Xcode 7.2 This code gives me an error that can't find the entity name.This as [Company] doesn't work.I have a entity with this name. let fetchRequest = NSFetchRequest(entityName: "Company") do { …
user2414590
  • 123
  • 1
  • 7
5
votes
3 answers

Changing NSFetchedResultsController's fetchRequest.predicate does not trigger delegate

I've got a NSPredicate on my FRC's fetchRequest. At some point an object is added to CoreData, I create a new predicate, update the fetchRequest and perform a fetch: self.fetchedAddressesController.fetchRequest.predicate = self.predicate; BOOL…
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
5
votes
2 answers

UITableView not updating DataSource after change to NSFetchedResultsController

I have an UITableView populated by a NSFetchedResultsController. The initial fetch works fine. I can add, remove, modify, etc with zero problems.. But I want to add user-defined sorting to the table. I am doing this by changing the…
5
votes
2 answers

FetchRequest - NSArray element failed to match the Swift Array Element type - Swift 2.0

I want to do a NSFetchRequest to display my data into a UICollectionView : import UIKit import CoreData let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let context: NSManagedObjectContext =…
Antonio
  • 135
  • 1
  • 12
5
votes
3 answers

Core Data fetch predicate nil check failing/unexpected results?

I have a Core Data layer with several thousand entities, constantly syncing to a server. The sync process uses fetch requests to check for deleted_at for the purposes of soft-deletion. There is a single context performing save operations in a…
Lytic
  • 786
  • 5
  • 12
5
votes
1 answer

How to fetch specific records in Core data

I am a new bid in iOS development. I am using NSManagedObject of Core Data to perform Insert and Fetch operations. It works perfectly fine. But the problem is, I want to fetch only some selected records (where condition in MySQL) from the table.…
Anu Padhye
  • 615
  • 1
  • 6
  • 16
5
votes
3 answers

Swift Core Data - Request with distinct results

how I can call es request with distinct values in swift? This is my code: let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context: NSManagedObjectContext = appDelegate.managedObjectContext let…
Maxim
  • 317
  • 1
  • 5
  • 13
5
votes
1 answer

RestKit and Managed Object Contexts

I'm trying to best format my project's use of RestKit and Core Data. There are a couple of things that I've got working, but I have a feeling they are implemented poorly and potentially thread unsafe... I have an object that manages all of my data…
Mike
  • 9,765
  • 5
  • 34
  • 59
5
votes
3 answers

core data - fetch attribute that match one of the values in an array

My core data model: Person ====== personId (NSNumber) This is a basic core data question, I have an array of personIds (not Person, just NSNumber of ids) and I want to fetch all the Persons with corresponding id in the array. This is how I…
Mario
  • 2,431
  • 6
  • 27
  • 34
5
votes
1 answer

Core data executeFetchRequest fail with exc_bad_access

From my understanding, this is a memory issue, especially since I call the method from several places several times with different timers. Code below that throw the exception: - (NSMutableArray*)getAllTraps { @synchronized(self) { …
Idan Moshe
  • 1,675
  • 4
  • 28
  • 65
5
votes
1 answer

Core Data NSFetchRequest with grouping

I have a fetch request that shoul result in a tableview with two sections from the result. My model: @interface Player : NSManagedObject @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSNumber * isFacebookFriend; The…
bogen
  • 9,954
  • 9
  • 50
  • 89
5
votes
1 answer

NSPredicate that filters out subclass results

I have two classes, Company and MyCompany. MyCompany is a subclass of Company, and Company is a subclass of NSManagedObject. I am trying to write a predicate for an NSFetchRequest that will return results of the class Company, but filter out…
Darren
  • 10,091
  • 18
  • 65
  • 108
5
votes
3 answers

Evaluate CoreData entity type in predicate

I have a block predicate that I carefully crafted only to discover you can't use them in Core Data. NSPredicate *rootContactPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { BOOL isPersonAndRoot…
Lee Probert
  • 10,308
  • 8
  • 43
  • 70
5
votes
5 answers

Core Data crash: Keypath Project not found in entity

- (void)fetchResult { NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Project" ascending:YES]; [request setEntity:self.entityDescription]; …
5
votes
1 answer

NSFetchRequest sum, group by and sorting

In my app I'm trying to list the four most popular products. The product popularity is determined by the total quantity in orders. The query below is to illustrate what I'm trying to do: SELECT TOP 4 SUM(quantity) as sumQuantity, Product FROM …
Yvo
  • 18,681
  • 11
  • 71
  • 90