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

Can NSFetchRequest propertiesToGroupBy be case insensitive?

I have following code for fetching and grouping search results: NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Song"]; [request setPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:predicates]]; [request…
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
6
votes
3 answers

NSFetchRequest without sort descriptors

We cannot use NSFetchRequest without providing NSSortDescriptor(s). All i want to do is fetch the results and show them in the order in which they were created. Is there a built-in way to do that?, or will i have to create a new "auto-increment"…
Mustafa
  • 20,504
  • 42
  • 146
  • 209
6
votes
6 answers

Core Data pattern: how to efficiently update local info with changes from network?

I have some inefficiency in my app that I'd like to understand and fix. My algorithm is: fetch object collection from network for each object: if (corresponding locally stored object not found): -- A create object if (a nested related…
Jaanus
  • 17,688
  • 15
  • 65
  • 110
6
votes
1 answer

Core Data Fetch requests slow on large data set

This is my first Core Data project, and I need advice on speeding up my fetch requests. My Core Data model contains 2 entities, Wells and Fluids. Wells has 50,000 records, and Fluids has 2 million records. They look like the following. Wells nams …
6
votes
1 answer

how to group by day with core data?

I have a Entity called deal and deal has a property called date which is the time this deal object inserted into the store. and one day may have several deals. So I want count some data group by day, I want fetch dayand…
Puttin
  • 1,596
  • 23
  • 27
6
votes
3 answers

NSDictionaryResultType expression not taking into account newly inserted objects

I want the maximum value of a field in core data so I have set up the following request: // Create fetch NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; [fetch setEntity:[NSEntityDescription entityForName:@"MyEntity"…
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
6
votes
1 answer

Core Data fetch request optimization

I'm developing and application which needs to check if a string has been saved to the database or not. This may seem an easy operation but it needs half a second to return any response which I think is quite a lot. My question is if there is any way…
IOS_DEV
  • 949
  • 3
  • 12
  • 33
5
votes
1 answer

How to avoid changing property values in an NSBatchInsertRequest?

I have a simple Core Data entity Story that occasionally I update with the latest data from a network call. This network call sometimes updates many, many stories instances, so I run an NSBatchInsertRequest, shown below. (The other reason I'm using…
5
votes
1 answer

FetchRequest Wrapped Value in SwiftUI : EXC_BAD_INSTRUCTION

I'm trying to do simple fetch request of my TrainingSessionHistory (coredata object) in one of my SwiftUI View but got Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) struct TrainingHistoryView: View { var trainingSession :…
Jopolaz
  • 402
  • 1
  • 3
  • 18
5
votes
0 answers

SwiftUI - Group NSFetchRequest by date to display in list with sections

I'm building a SwiftUI app where it shows a list of values containing their associated date and duration. At the moment the ContentView is just listing all the values (date ascending). What I would like to achieve is so that entities are grouped by…
WH48
  • 737
  • 1
  • 7
  • 24
5
votes
3 answers

NSFetchRequest fetchBatchSize not working

I have been having problems with fetchBatchSize in an NSFetchedResultsController so I decided to take a step back and use it on just a plain NSFetchRequest. I created a very simple project with only a tableViewController that has 100 items. The…
alionthego
  • 8,508
  • 9
  • 52
  • 125
5
votes
1 answer

Retrieve array (Transformable) from core data Swift 4

I have an Entity called "Item" with an attribute called "colorArray" with type "Transformable". colorArray could be, for example: [["Red", "Blue", "Green"], ["Red"], ["Blue", "Green"], ["Green"], ["Blue"], ["Blue", "Green", "Red"]] I then save…
user3227546
5
votes
1 answer

Swift - CoreData NSPredicate - Fetch Children of parent

I'm trying to fetch all the children of a parent. In my case the parent is an entity AgendaEvent which has many AgendaDate (the children). so Here's my function: func getRelatedAgendaEvent(event: AgendaEvent) ->NSFetchRequest { //…
Marco
  • 1,051
  • 1
  • 17
  • 40
5
votes
1 answer

Swift: Search for string in core data model

I'm confronted with the following problem in Swift. I wrote the following function to retrieve all persons in my model with the name "David". private func myFetchRequest() { let moc = (UIApplication.sharedApplication().delegate as!…
Pisan
  • 69
  • 2
  • 10
5
votes
2 answers

Core Data: keypath name not found in entity

I'm crashing with this message : 'NSInvalidArgumentException', reason: 'keypath name not found in entity Obvisouly I'm not querying my entity correctly . //fetching Data NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]…
Finger twist
  • 3,546
  • 9
  • 42
  • 52