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
7
votes
1 answer

How to limit the number of results in a FetchRequest in SwiftUI

How can I limit the size of the retrieved FetchedResults when making a FetchRequest to CoreData? struct ContentView: View { var fetchRequest:FetchRequest init(){ fetchRequest = FetchRequest(entity: Kana.entity(),…
simibac
  • 7,672
  • 3
  • 36
  • 48
7
votes
2 answers

How to rerun @FetchRequest with new predicate based on user input?

I've a list displaying object from CoreData using @FetchRequest, I want to provide the user with a bar button that when clicked will filter the displayed list. How can I change the @FetchRequest predicate and rerun it dynamically to rebuild the list…
M.Serag
  • 1,381
  • 1
  • 11
  • 15
7
votes
1 answer

Is it possible to sort by subclasses in an `NSFetchRequest` without adding additional attributes?

I want to group the results of a NSFetchRequest by entity. The entities all share the same abstract parent. For example: animal | |-cat | |-dog The NSFetchRequest has includesSubentities set TRUE and entity set to animal. It is possible to set…
Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
7
votes
1 answer

why is NSFetchedResultsController's fetchedObjects array not always homogeneous

So that I don't bury the lede, I'm going to open with my core question: why is it that my NSFetchedResultsController's fetchedObjects array is usually homogeneous, but on rare occasions contains an __NSCFString among the managed objects it should…
pohl
  • 3,158
  • 1
  • 30
  • 48
7
votes
1 answer

Not found an object in coredata after added to a relationship

I have an issue with the fetch of an object after added in a relationship. The first time that i fetch the category, always found, then when i added to the relationship the following categories not found. The relationship is a…
brunobasas
  • 383
  • 2
  • 14
7
votes
1 answer

Can I set transient properties to fetch?

I want to create NSFetchRequest and set properties to fetch like this: request.propertiesToFetch = @[@"a", @"b", @"c"]; where a and b are stored in Core Data database, and c is transient. executeFetchRequest: fires an error Invalid keypath c passed…
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
7
votes
2 answers

CoreData ordered relationships - batch unfaulting using NSFetchRequest

Background - Batch unfaulting: NSFetchRequest allows batch unfault - for example, use a query of 1000 results, it would bring all as faults, then it would unfault X objects at a time (i.e. index 0-20, then 21-40, etc) This behavior is great when…
7
votes
1 answer

fetch request for entity.attribute == @"somevalue"

How do I setup a fetch request to only pull the data from an entity's attribute with one particular value? This is the basic code I've used before. -(void)fetchResults { NSFetchRequest *fetchRequest = [NSFetchRequest…
jpgr
  • 278
  • 2
  • 3
  • 13
7
votes
4 answers

Improve speed for updating existing records (~11.000) in Core Data

I'm parsing a ton of data which I initially insert into a core data store. At a later point, I am parsing the same XML, though some of it may have been updated. What I then do is check for an existing record with the same tag and if one already…
runmad
  • 14,846
  • 9
  • 99
  • 140
6
votes
1 answer

Core Data NSFetchRequest returns unsorted array after deleting object and refetching data

I have a Core Data database built using a UIManagedDocument that I load into a UITableView and also plot certain points of that data on a graph. I find that when I add an object to the database or delete an object from the database and then fetch…
Jamie
  • 5,090
  • 28
  • 26
6
votes
1 answer

Is having a NSFetchRequest with an NSPredicate supported in MonoTouch

I have tried the following using an NSPredicate and am not getting the results I would expect: NSFetchRequest request = new NSFetchRequest (); request.Entity = NSEntityDescription.EntityForName("Entity", managedObjectContext); NSSortDescriptor sort…
cmour
  • 598
  • 6
  • 13
6
votes
1 answer

Core Data search optimization

I'm working on a search feature in one of my Core Data based apps and I'm trying to gather everyone's tips on search optimization to get it as fast as I possibly can. The search needs to be fast enough that it can deliver near-instantaneous results…
indragie
  • 18,002
  • 16
  • 95
  • 164
6
votes
1 answer

CoreData Predicate get every sentence that contains any word in array

In Swift 4 I have a CoreData "Sentence" model that has a String attribute "englishsentence". I also have an array of "words" and would like to fetch all sentences for which the "englishsentence" attribute contains one or more of the words in the…
Sebastian
  • 445
  • 5
  • 20
6
votes
1 answer

Access FetchRequest from xcdatamodel in iOS

I'm using NSPersistentContainer to access my core data in iOS10 app and Xcode 8. Similar to Entity, I added a "UserFetchRequest" in my Coredata xcdatamodel. Below is the screen shot. How can I access the "UserFetchRequest" in the code?
Satyam
  • 15,493
  • 31
  • 131
  • 244
6
votes
3 answers

How to fetch core data relationships in one single NSFetchRequest?

I have a core data app with the model setup just like the image below: The relationships are setup like this: Category<-->>Subcategory<-->>Item Now, is it possible in one single fetch to get objects from Subcategory and Item entities that have the…
Marco Almeida
  • 1,285
  • 3
  • 17
  • 39