In Objective-C, using core data, I was fetching entities and they were returned as NSArrays. I realized that I was fetching too often and I could make use of the entity’s return values, for example: Customer entity has many Invoices, Invoices have many ItemSolds. Here is some code I’m using:
NSError *error = nil;
// fetch all customers
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
self.fetchedCustomers = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedCustomers == nil) {
NSLog(@"ERROR");
}
[fetchRequest release];
// end of customer fetch
This is the simple fetch request, and fetchedCustomers is set as an NSArray property. I then use it’s functions :
self.fetchedInvoices = [[customerToView valueForKey:@"invoices"] allObjects];
This works, and I’m able to display invoice numbers and dates into a table properly. However I then go on to use :
self.fetchedObjects = [[fetchedInvoices valueForKey:@"itemsSold"] allObjects];
Later, when I try to add totals I do the following:
double price = [[[fetchedObjects objectAtIndex:i] valueForKey:@"Price"] doubleValue];
And I get the following error:
-[__NSCFSet doubleValue]: unrecognized selector sent to instance 0x10228f730
Why is there an NSSet involved here at all? When I fetched the invoices and items using predicates, I didn’t have any problems, but it seemed really inefficient. I would much rather figure out what’s going wrong here. Any help would be appreciated, thanks.
Extra info:
Areas of interest:
@interface Invoice : NSManagedObject {
@private
}
@property (nonatomic, retain) NSSet *itemsSold;
@end
@interface Invoice (CoreDataGeneratedAccessors)
- (void)addItemsSoldObject:(ItemSold *)value;
- (void)removeItemsSoldObject:(ItemSold *)value;
- (void)addItemsSold:(NSSet *)values;
- (void)removeItemsSold:(NSSet *)values;
@end
I realized my error. I was sorting the fetchedCustomers into a specific “customerToView”. I did not do the same thing with my fetchedInvoices. So my solution as to add :
And use invoiceToView in the place of fetchedInvoices.
Foolish error on my part!