Retrieve single object from NSFetchRequest - iphone

I want to get a single object from my Core Data datastore, here is the code I have been using but it returns an array of objects. There must be a simpler and better way:
NSFetchRequest *request= [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Manufacturer" inManagedObjectContext:managedObjectContext];
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"ManufacturerID==%#",[[mitems objectAtIndex:i] objectForKey:#"ManufacturerID"]];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *error;
NSArray *entities = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

If your predicate always fetches more than one result:
refine the predicate - don't forget you can build predicates with logic like AND/OR, simple equality is easy but may not be selective enough in your case.
just select the result you want from the array, it's no big deal - although if this is possible it should also be possible to refine the predicate...
consider reorganizing your data model so that you can refine the predicate to get just one item back.
The fetch always returns an array, that is its definition. However it can be an array of one object.

It will always return an array but you can make it cleaner:
NSFetchRequest *request= [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Manufacturer" inManagedObjectContext:managedObjectContext];
NSPredicate *predicate =[NSPredicate predicateWithFormat:#"ManufacturerID==%#",[[mitems objectAtIndex:i] objectForKey:#"ManufacturerID"]];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *error;
//Making a mutable copy here makes no sense. There is never a reason to make this mutable
//NSArray *entities = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
id manufacturer = [[managedObjectContext executeFetchRequest:request error:&error] lastObject];
request = nil;
NSAssert1(error && !manufacturer, #"Error fetching object: %#\n%#", [error localizedDescription], [error userInfo]);
-lastObject will return the last item in the array or nil if the array is empty. This keeps your code a little cleaner when you know there is going to be one object in the array or if you don't care what object you pull out of the array.
BTW, your property names should start with a lower case letter. I am surprised the compiler did not warn you about that.

Try fetchRequest.fetchLimit = 1. You still get an array, but with a maximum of 1 object.

Related

How to sum on a calculated property?

I'm sure I'm guilty of trying to apply SQL logic to Core Data, but even after reading the Apple docs I'm still not sure of the correct approach.
What would be the best strategy for summing the difference of two date properties in a Core Data entity? I have a "numHours" calculated property on the "appt" class but not in the xcdatamodel. The code below fails with a "keypath not found in entity" error. Many thanks for any advice.
from appt class
- (float)numHours {
float hours = ([self.endTime timeIntervalSinceDate:self.startTime] - [self.durationOfBreak intValue]) / 3600.00;
return hours;
}
attempt at fetching sum of "numHours" property
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"appt" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSPredicate *datePredicate = [NSPredicate predicateWithFormat:#"date >= %# && date < %#", self.startDate, self.endDate];
[request setPredicate:datePredicate];
[request setResultType:NSDictionaryResultType];
NSExpression *numHoursPath = [NSExpression expressionForKeyPath:#"numHours"];
NSExpression *hoursSum = [NSExpression expressionForFunction:#"sum:"
arguments:[NSArray arrayWithObject:numHoursPath]];
NSExpressionDescription *debitExpressionDescription = [[NSExpressionDescription alloc] init];
[debitExpressionDescription setName:#"totalhours"];
[debitExpressionDescription setExpression:hoursSum];
[debitExpressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:debitExpressionDescription]];
[debitExpressionDescription release];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
Jeffery,
You cannot fetch based upon an attribute that isn't actually in the model. The error message, "keypath not found in entity," plainly says this. You can confirm this for yourself by browsing the SQLite DB and you'll see there is no attribute for the SQL to query. Hence, your fetch request must fail. NSExpression *numHoursPath is a valid expression. It can be applied to collections after you've fetched them. You just cannot use them in a fetch request.
Andrew

Quick Question About NSFetchRequest and Relationship

In my Core Data model, I have an entity Session and Exercise.
Session has a to many relationship to Exercise (there is a one-one inverse relationship as well).
In my fetch, I am trying to find all Session object that are related to the current Exercise.
I am using the following code which isn't working.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat: #"exercise = %#", exercise.name]];
NSEntityDescription *sessionEntity = [NSEntityDescription entityForName:#"Session" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:sessionEntity];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(#"Fetch error: %#", error);
self.sessionArray = results;
Here is my data model:
First, from the screenshot it seems that your relationship attribute of the Session entity is called exercises not exercise.
Also, it seems to me that it would work if you searched not the Session entity but the Exercise entity and then iterate through the resulting array to extract the sessions.
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:"name == %#", exerciseName]];
NSEntityDescription *exerciseEntity = [NSEntityDescription entityForName:#"Exercise" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:exerciseEntity];
...
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
...
NSMutableArray *mutableSessionArray = [NSMutableArray array];
for (Exercise *ex in results) {
Session *session = [ex exercises];
if (session) [mutableSessionArray addObject:session];
}
self.sessionArray = [NSArray arrayWithArray:mutableSessionArray];
The relationship of the Exercise entity called exercises could be called session for clarity. I would recommend renaming it.
BTW, you can also write = instead of ==, they are equivalent, as far as I know. In the Predicate Programming Guide section on Predicate Format String Syntax it says:
=, ==
The left-hand expression is equal to the right-hand expression.
In your predicate you use =, == is the comparison operator. I think that's where you're going wrong.
Source: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

Using NSPredicate to get an array of Core Data objects?

Say I have a Core Data entity called Person. How would I get an NSArray of Persons whose properties match certain values? For instance someone of a particular age, height, or weight... or someone with a whose height,weight and age are specific values...
Can I use an NSPredicate like so:
NSPredicate *pred =
[NSPredicate predicateWithFormat:
#"(age == 25) OR (height_in_cms == 185) OR (age == 30 AND height_in_cms == 170 AND weight_in_kgs == 80)";
// All properties are NSNumber
I'm not an expert on the syntax for predicateWithFormat:, but you have the basic gist. You can find details on the format in Apple's Predicate Programming Guide. If you're asking what to do with the predicate once you have it, here is a snippet that shows you the steps:
// Create a fetch request.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Set the entity for the fetch request.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"EntityName" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[entity release];
// Set the predicate for the fetch request.
[fetchRequest setPredicate:predicate];
// Perform the fetch.
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
If you want the results to be sorted, you can pass an array of sort descriptors to the fetch request using setSortDescriptors: prior to executing the fetch.
You can follow the given statement if you have these value in a variable.
[fetchResults filterUsingPredicate:[NSPredicate predicateWithFormat:#"age == %i OR hieght== %i AND weight==%i",age,height,weight]];
And also your approach is correct in case for specific values but your statement having syntax error so maintain proper syntax

How to determine number of objects in one-to-many relationship in CoreData

So, I've got a one-to-many relationship of Companies to Employees in CoreData (using a SQLite backend on iOS, if that's relevant). I want to create a predicate that only returns Companies that have 0 Employees associated with them. I could do it by getting all the Companies and iterating over them, but that would be (I assume) much slower.
Any ideas?
Thanks,
-Aaron
After trying #falconcreek's answer and getting an error (described in my comment on his answer), I did some googling and determined that the answer was
NSPredicate *noEmployeesPredicate = [NSPredicate predicateWithFormat:#"employees.#count == 0"];
Now everything works über efficiently. Thanks!
Assuming your Company -> Employee relationship is named "employees"
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Company" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
// the following doesn't work
// NSPredicate *noEmployeesPredicate = [NSPredicate predicateWithFormat:#"employees = nil OR employees[SIZE] = 0"];
// use #count instead
NSPredicate *noEmployeesPredicate = [NSPredicate predicateWithFormat:#"employees = nil OR employees.#count == 0"];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (error)
{
// Deal with error...
}

How to Determine if a Table Contains Any Records with Core Data?

I haven't seen any other questions quite like this on here, but I'm hoping someone has some insight. I'm just starting to learn Core Data.
Basically, I have two methods and I want to choose which one to call with an if/else statement based on whether or not the "Contacts" table contains any records. Is there a way using core data to check if there are any records in a table?
The best way I've found so far is to set the fetchLimit to 1 and then check to see if anything returns.
[request setFetchLimit:1];
But I keep thinking there has to be a better/easier way. Anyone know or have a good reference I can look at?
Thanks a ton!
Yes, definitely there is a better method. Setup a fetch request as usual, but, instead of actually executing it, simply ask for the number of objects it would have returned if it had been passed to executeFetchRequest:error:
This can be done using
- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
Something like this:
- (int) numberOfContacts{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = yourManagedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contacts" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
[request release];
if (!error){
return count;
}
else
return -1;
}
It's not necessarily any better or easier, but you can look for a specific record and then create it if it doesn't exist like this:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contact"
inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error;
// Filter based on a predicate
[fetchRequest setPredicate:
[NSPredicate predicateWithFormat:#"identifier == %#", #"1"]];
NSManagedObject *contact = [[managedObjectContext
executeFetchRequest:fetchRequest error:&error] lastObject];
// If the contact was not found
if (!contact)
{
// Create the contact
contact = [NSEntityDescription insertNewObjectForEntityForName:#"Contact"
inManagedObjectContext:managedObjectContext];
[contact setValue:[NSNumber numberWithInt:1] forKey:#"identifier"];
[managedObjectContext save:nil];
}
Marcus Zarra posted some code that demonstrates this in a feed reader app. Marcus is the Core Data master.