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

invalid json response body error with Express node-fetch using formData

I'm trying to do http request from nodeJS, this is the following request: const form = new FormData(); form.append('text 1', 'sometext'); form.append('file', fs.createReadStream("foo.txt")); fetch('url', { method:…
User100696
  • 687
  • 4
  • 12
  • 26
14
votes
1 answer

How to use predicates with fetchRequest in Core Data

I pass a contact Identifier from Contacts tableview controller to another Location tableview controller. So I define a delegate ContactSelectionDelegate and implement method userSelectedContact in Location tableview controller and get…
vrao
  • 545
  • 2
  • 12
  • 33
13
votes
1 answer

Sorting NSFetchedResultsController by Swift Computed Property on NSManagedObjectSubclass

I'm building an app using Swift with Core Data. At one point in my app, I want to have a UITableView show all the objects of a type currently in the Persistent Store. Currently, I'm retrieving them and showing them in the table view using an…
Michael Hulet
  • 3,253
  • 1
  • 16
  • 33
12
votes
2 answers

Sort NSFetchRequest by date and then by alphabetical order

I want to order a NSFetchRequest first by date and then, if it matches the same day order by name. I use a UIDatePicker to get the date and the save it using Core Data [self.managedObject setValue:self.datePicker.date forKey:self.keypath]; and sort…
android iPhone
  • 860
  • 2
  • 11
  • 22
12
votes
6 answers

iPhone OS: Fetching a random entity instance using NSPredicate Nsfetchrequest and core data

Working on an app where I have a large collections of managed objects against which I want to fetch a few random instances. My question is, is there any way I can use NSPredicate and NSFetchRequest to return several objects at random. I saw that you…
nickthedude
  • 4,925
  • 10
  • 36
  • 51
12
votes
1 answer

Core Data NSPredicate fetch on entity relationship using in clause

I would like to be able to search the relationship of an entity using an IN clause. I have the following setup: I would like to send over an array of nicknames and find all Persons associated with those nicknames. //array of names to find let…
slidmac07
  • 387
  • 1
  • 2
  • 14
12
votes
2 answers

coredata - fetch one attribute into an array

Aim: I would like to fetch the value of one attribute (from an entity) from the database (core data) into an array. Example Entity Name = Employees Attribute = employeeID I just want all the employeeIDs populated into an array /…
user1046037
  • 16,755
  • 12
  • 92
  • 138
12
votes
2 answers

Core data: executeFetchRequest vs performFetch

I want a thorough list regarding comparison between the two. Things I have known: executeFetchRequest: Message sent to MOC Return an array of managed objects Goal: fetch objects from persistent store to MOC With table view: has nothing to do with…
12
votes
1 answer

NSInvalidArgumentException', reason: 'Unknown predicate type for predicate: BLOCKPREDICATE(0x70ad750)' Error

I have a core data database and I am trying to create a fetch request using a block predicate, but I get an Unknown Predicate error: NOTE: employeeToHouse is a property of type House that was created for me when I subclassed…
Ilya Lapan
  • 1,103
  • 2
  • 12
  • 31
11
votes
4 answers

Core Data - How can I get the max value from an entity attribute (Swift)

Recipe recipeID: Int recipeName: String I have an entity Recipe with an attribute recipeID. How can I get the max(recipeID) as an Int value in Swift? I'm new in swift, please help me. Thanks in advance. func fetchMaxID() { let context =…
Michael
  • 293
  • 3
  • 12
10
votes
2 answers

Swift 3 - NSFetchRequest Distinct Results

Any help appreciated. Xcode auto updated to 8... I am Targeting IOS 9.3 Have all of the code converted across but one thing is now breaking, I have tried various suggestions in similar questions! My fetch request that was previously working is…
MyName
  • 101
  • 1
  • 5
10
votes
2 answers

countForFetchRequest in Swift 2.0

I am trying to use the countForFetchRequest method on a managed object context in Swift 2.0. I note that the error handling for executeFetchRequest has been changed across to the new do-try-catch syntax: func executeFetchRequest(_ request:…
Skoota
  • 5,280
  • 9
  • 52
  • 75
10
votes
1 answer

How to make an NSFetchRequest which asks for objects that have a specific firstname?

For example, I have a Managed Object Model with an Entity called "Friends", and a friend has a firstName. I want to get all friends where the firstName is equal to "George". How can I do that?
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
9
votes
1 answer

What is an efficient way to get an array of property values from a core data entity?

Consider the following: NSFetchRequest *request = [[NSFetchRequest Alloc] init]; request.entity = [NSEntityDescription entityWithName:@"Person" inContext:_MOC]; request.propertiesToFetch = [NSArray arrayWithObject:@"Name"]; NSError *error =…
XJones
  • 21,959
  • 10
  • 67
  • 82
9
votes
2 answers

How to fetch array of objects using array of ids using core data?

I have an array of ids. I want to fetch core data entity for the items in the ids array. How can we implement that using NSFetchRequest?
Sheik_101
  • 770
  • 1
  • 8
  • 24
1
2
3
74 75