views:

124

answers:

1

Hello,
I have difficulties displaying objects coming from CoreData in a tableView. I have 2 sorts of entities : Sample and SampleList. What is important to know is that a SampleList has an attribute sampleSet which is a set of samples (entity of Sample)

First I succeeded in displaying every SampleList. Here is viewDidLoad:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SampleList" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastSampleDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

if (mutableFetchResults == nil) {
// Handle error
}

[self setSampleListArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

Once I click on a row in my tableView, I would like to display in another UITableView every sample from the SampleList selected.

I thought that I could pass to the subview SampleList mySampleList. But then, I don't know what to do with it as it is not organized. How can I return an ordered array of Sample (ordered by dateSample attribute for example) ?

Thank you for your time !

+3  A: 

You can just use NSSet and NSArray methods on sampleSet to get an ordered array:

sortedArray = [[sampleSet allObjects] sortedArrayUsingSelector:@selector(compare:)];

or if you want to specify particular sort descriptors instead of the regular "compare" method:

sortedArray = [[sampleSet allObjects] sortedArrayUsingDescriptors:sortDescriptors];
David Gelhar
It worked ! that was fast actually. But I don't understand why I had to retain sortedArray (which is declared in the .h)
Leo