views:

48

answers:

1

Hi all.

Maybe there is a simple solution to this, but I'm getting headache of this, I'm fairly new with all this Core Data stuff:

I have a BankAccount class/entity with an "index" attribute, used for sorting, and a "transactions" to-many relationship to the Transaction class/entity. This Transaction entity has a "date" attribute, that I want to use for sorting too.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"BankAccount" inManagedObjectContext:self.managedObjectContext]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

This works well and delivers me the BankAccount objects nicely sorted by "index". But, each BankAccount object contains a NSSet "transactions" that is, ofcourse, not sorted at all. How can I get these transactions sorted by the "date" attribute, and is this possible within the same fetch request?

Many thanks in advance.

A: 

To do this you would need to fetch the Transactions ordered.

What you would need is:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Transaction"
                                    inManagedObjectContext:self.managedObjectContext]]; 
[fetchRequest setPropertiesToFetch :[NSArray arrayWithObjects:@"date", @"bankAccount", nil]];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:
                                  [[[NSSortDescriptor alloc] initWithKey:@"date"
                                                               ascending:YES
                                                                selector:@selector(compare:)] autorelease],
                                  [[[NSSortDescriptor alloc] initWithKey:@"bankAccount.index"
                                                               ascending:YES] autorelease],
                                  nil]];

I'm assuming that Transaction is the name of the entity for transactions, and that bankAccount is the relationship from entity Transaction to BankAccount.

vfn
Where you able to get the expected result?
vfn