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

Swift 3. NSFetchRequest propertiesToFetch

In Swift 3 when we use NSFetchRequest, we have to specify NSFetchRequestResult. But how to get an array of properties values? If I use something like this let fetchRequest = NSFetchRequest(entityName:…
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
4
votes
4 answers

Swift3: Passing parameters into NSFetchRequest method

I use a general CoreData query method in my project. func query(table: String, searchPredicate: NSPredicate) -> [AnyObject] { let context = app.managedObjectContext let fetchRequest = NSFetchRequest(entityName: table) …
iphaaw
  • 6,764
  • 11
  • 58
  • 83
4
votes
1 answer

NSFetchRequest Core Data Swift 3 Backward compatibility

I have converted my code in swift 3. I am using core data in my application. As you know, NSFetchRequest has been changed. In swift 2 it was: let request = NSFetchRequest(entityName: "UnsyncedTask") In swift 3: let…
Rox
  • 909
  • 11
  • 31
4
votes
2 answers

Core Data: Fetch via specific property (join relationship)

I have an core data model as follows The attributes property of Page is a set of DictionaryEntry, they are values for my Page objects, much like a standard NSDictionary (except all of the keys and values are strings) I have a Page that has a…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
4
votes
3 answers

Grouping a dictionary NSFetchRequest by object ID

I need to return a list of objects along with a count of its related objects. It doesn't seem to be possible to do this in a single dictionary fetch request as I am unable to group the fetch results by objectID. let objectIDExpression =…
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
4
votes
1 answer

core data and paging

I have a database of 50,000 records. I'm using core data to fetch records from a search. A search could return 1000 records easily. What is needed to page through these records using core data and uitableview? I would like to show 100 records…
Joo Park
  • 3,115
  • 4
  • 27
  • 31
4
votes
0 answers

Effect of affectedStores on fetching unsaved NSManagedObjects?

Here is a brief sequence that involves a Core Data stack with an (initially empty) NSManagedObjectContext and a NSPersistentStoreCoordinator with two NSPersistentStores: Player *player = [NSEntityDescription insertNewObjectForEntityForName:…
Drux
  • 11,992
  • 13
  • 66
  • 116
4
votes
1 answer

How to fetch managed objects sorted by calculated value

I'm working on the app that uses CoreData. There is location entity that holds latitude and longitude values. I'd like to fetch those entities sorted by distance to the user's location. I tried to set sort descriptor to distance formula sqrt ((x1 -…
Marcin Zbijowski
  • 820
  • 1
  • 8
  • 23
4
votes
3 answers

core data fetch last 20 records in ascending order?

In my app i've some records in core data and i want to fetch last 20 records in ascending order i.e.. if there is 30 records than i want to fetch records from 11 to 30 in ascending order - NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]…
Blios
  • 719
  • 1
  • 16
  • 30
4
votes
2 answers

Is there a faster way to retrieve a specific managedObject in Core Data than a fetch request?

I have a method to create managed objects (IntersectionObject) each with three properties. These three properties are managed objects themselves. PropertyObject1, PropertyObject2, and PropertyObject3 each has about 20 different possibilities. An…
Michael Campsall
  • 4,325
  • 11
  • 37
  • 52
4
votes
2 answers

NSFetchedResultsController multiple entities for UITableView

I have two entities one called Post and one called User. Post<<---->User is the relationship in core data. I am using a NSFetchedResultsController to fetch all Post records in my core data stack and then displaying them in a UITableView. Each…
4
votes
1 answer

iOS: natural sort order

I have an app for iOS that uses Core Data to save and retrieve data. How would I fetch data sorted by a field of NSString type in natural sort order? Right now the result is: 100_title 10_title 1_title I need: 1_title 10_title 100_title
surlac
  • 2,961
  • 2
  • 22
  • 31
4
votes
1 answer

How to make FetchRequest in callback block asynchronously

Does anyone know the best practice that how to start another new asynchronous method in the completion block of the first asynchronous communication? I am testing the code to make a call NSFetchRequest(coz STACKMOB iOS SDK internally sync with…
4
votes
2 answers

Disappointing iOS search times with CoreData

I have a coredata db running on an ipad with iOS 5.1.1. My database has around 50,000 companies in it. I have created an index on the company name attribute. I am searching on this attribute and on occasion get several thousand records returned…
4
votes
4 answers

NSSortDescriptor should sort NSDates

It appears NSSortDescriptor should be a fairly easy class to work with. I have stored in CoreData an entity with an attribute of type NSDate appropriately named @"date". I am attempting to apply a sort descriptor to a NSFetchRequest and it doesn't…
esreli
  • 4,993
  • 2
  • 26
  • 40