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
128
votes
3 answers

How to get Core Data object from specific Object ID?

I can easily get an object's ID in Core Data using the following code: NSManagedObjectID *moID = [managedObject objectID]; However, is there a way to get an object out of the core data store by giving it a specific object ID? I know that I can do…
Jason
  • 14,517
  • 25
  • 92
  • 153
45
votes
3 answers

CoreData get distinct values of Attribute

I'm trying to setup my NSFetchRequest to core data to retrieve the unique values for a specific attribute in an entity. i.e. an entity with the following information: name | rate | factor | _______|______|________| John | 3.2 | 4 | Betty…
ephilip
  • 1,015
  • 1
  • 10
  • 23
39
votes
6 answers

SwiftUI View and @FetchRequest predicate with variable that can change

I have a view showing messages in a team that are filtered using @Fetchrequest with a fixed predicate 'Developers'. struct ChatView: View { @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Message.createdAt, ascending: true)], …
user1242574
  • 1,257
  • 3
  • 12
  • 29
39
votes
5 answers

Core data, how to get NSManagedObject's ObjectId when NSFetchRequest returns NSDictionaryResultType?

I have an NSFetchRequest which is returning the objects' properties in an NSDictionaryResultType. Is it possible to also get the objects' ObjectId within this dictionary? Otherwise I will need to run the query with a return type of…
Jason
  • 14,517
  • 25
  • 92
  • 153
37
votes
3 answers

Can I apply multiple predicates to an NSFetchRequest? Would it be better to manually parse my results?

Ok I have a basic iPad app that asks for 5 search/filter criteria from the user. Based on this data, I need to go to my core data db, and pull out any managed objects that fit that criteria. It seems like I need to apply more than one predicate to…
Lizza
  • 2,769
  • 5
  • 39
  • 72
30
votes
2 answers

How do I make a fetch request using NSManagedObject's new fetchRequest function?

In iOS 10 the CoreData team added a new "fetchRequest" method to NSManagedObject. It looks like this: public class func fetchRequest() -> NSFetchRequest Which, from what I understand, allows us to replace this: let request =…
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
26
votes
1 answer

Why use NSFetchedResultsController?

Core data provides the method "executeFetchRequest" in NSManagedObjectContext class, which we can use to fetch data from tables and use it whatever the way need to. Now there is another way by using NSFetchedResultsController and providing it to…
Ansari
  • 1,907
  • 2
  • 23
  • 34
26
votes
2 answers

NSFetchRequest and predicateWithBlock

I am playing with an app that uses Core Data and NSManagedObjects to populate a UITableView. There is only one class in my application, called Event. I have created the following custom instance method on Event: - (BOOL)isExpired { return…
23
votes
4 answers

How to use binary flags in Core Data?

I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001"? I'm also wondering if…
CodeFlakes
  • 3,671
  • 3
  • 25
  • 28
21
votes
8 answers

iOS CoreData NSPredicate to query multiple properties at once

I am trying to use a UISearchBar to query multiple properties of a NSManagedObject I have a NSManagedObject called Person, every person has a name and socialSecurity property. Right now my code can perform a search (fetch) for one of those…
OscarTheGrouch
  • 2,374
  • 4
  • 28
  • 39
20
votes
1 answer

NSSortDescriptor sorting using NSDate in Swift

How would I sort a NSFetchRequest with that the date property of the managed object. So that it creates a array with the dates going in order? Here is my code so far... var request : NSFetchRequest = NSFetchRequest(entityName: "History"); …
Daniel K.
  • 1,326
  • 2
  • 12
  • 18
19
votes
3 answers

Using FetchRequest in swiftUI view model

I'm trying to follow the MVVM pattern in swiftUI and I’m running into a problem with core data and fetch request. All of the videos I've seen and articles I have read on it, have a @FetchRequest in the view, that accesses and modifies the core data.…
Richard Witherspoon
  • 4,082
  • 3
  • 17
  • 33
17
votes
4 answers

How do fix the "Cannot find 'NSFetchRequest' in scope error

I have tried to make an NSFetchRequest in many different ways and each time I get this error: "Cannot find type 'NSFetchRequest' in scope" Here are the specific ways I have tried: let fetchRequest: NSFetchRequest =…
Mark Reggiardo
  • 467
  • 2
  • 4
  • 15
16
votes
4 answers

Core Data: Keypath Error Not Found in Entity

Could any one tell me what's the wrong with this code? It raises the following error and cause application to crash: reason: 'keypath Studies.patients.PatientName not found in entity ' Code: - (void)viewDidLoad { …
Ali
  • 1,975
  • 5
  • 36
  • 55
15
votes
3 answers

Case Insensitive Compare with Core Data and Swift

The following code doesn't work: let sortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector:"caseInsensitiveCompare") And gives the following error: 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector:…
Gaurav Sharma
  • 2,680
  • 3
  • 26
  • 36
1
2 3
74 75