2

I used KVC until now to access object's properties.

In my object i have a method like this:

-(Address *)mainAddress {
    if (self.addresses != nil) {
        return [self.addresses anyObject]; //stub method
    }
    else {
        return nil;
    }
}

I can use this method with KVC using

mystring = [cliente valueForKeyPath:@"mainAddress.city"];

but i cannot use to create a NSFetchRequestController (this code use MagicalRecord)

NSFetchedResultsController *acontroller = [Customer fetchAllSortedBy:@"mainAddress.city" ascending:ascending withPredicate:companyPredicate groupBy:nil delegate:self];

This is the error:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath mainAddress.city not found in entity <NSSQLEntity Customer id=4>'
laxonline
  • 2,657
  • 1
  • 20
  • 37
IgnazioC
  • 4,554
  • 4
  • 33
  • 46

1 Answers1

1

In order to use the sorting with an NSFetchedResultsController, your mainAddress keyPath needs to be an attribute on your entity. NSFRC will sort the data not using KVC in memory, but using the underlying data store. Bottom line answer: make mainAddress a field on your entity in the data model.

casademora
  • 67,775
  • 17
  • 69
  • 78
  • I Will try but i don't like add duplication in db. – IgnazioC Jan 13 '13 at 11:04
  • this is not a matter of duplication, but rather figuring out why your search is crashing. Which would you rather have, duplicate data, or a crashing app? – casademora Jan 13 '13 at 18:51
  • i changed the method with a simply property, now the NSFRC works perfectly, even MagicalRecord...but i need to change my model. – IgnazioC Feb 01 '13 at 13:00