开发者

iOS Core Data - fetching results

My data model is multi-tiered . Let me give an example to demonstrate the data model.

I have three entities say X , Y and Z .

X could have 1 or more Y's.

Y is associated only with 1 X.

Y could have 1 or more Z's.

Z is associated only with 1 Y .

Now my top tableview controller - Controller A uses a fetchedResults controller to get me all the X's from core data

When the user selects a particular row , he/she selects a particular X from which I have the list of all Y's under this X . A particular Y which is passed to controller B . I also pass the managed objected context from A to B .

In B I want to be able to display all the Z's associated with the Y but in some order . Currently I find that the order is indicative of the Most recently used Z . I would want it in the reverse order .

How do I specify ( in terms of core data) to be able to act upon this specific Y and get me results based on a particular criterion ? All the examples for core data that i ve seen so far start from scratch as shown below

NSManagedObjectContext *context = <#Get the context#>;

NSFetchRequest *fetchRequest = [[NSF开发者_高级运维etchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"<#Entity name#>"
    inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>"
    ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    // Handle the error
}

[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

One solution to this is to NOT use NSFetchedRequest but to act upon the passed Y object and sort the NSArray .

NSAssert1(yObject!=nil,@"yObject is null",1);
NSSet *zObjects = yObject.zobjectsinY;
NSArray *zObjectArray = [zObjects allObjects] ; 
sortedQZArray = [zObjectArray sortedArrayUsingFunction:intSort context:NULL];

I feel that this is not an elegant solution and there should be a better way to query for these results .

Also another option is to be able to insert these values in the expected order when creating the data ? How can I specify the order for a record that is to be inserted in an entity object ?

Thanks in advance


If your getting the reverse of the sort order you want, just reverse the accending; parameter of the sort.

How can I specify the order for a record that is to be inserted in an entity object ?

You can't. Sorting is considered an attribute of the interface (because the same data may appear in with many different orders) so managed objects are kept in unordered sets until you fetch them with a sort descriptor.

If you want to save some kind of order in the data, you have to model the order using an attribute or relationship. Usually, however, you want to keep the order "virtual" in that it only appears as needed in the UI.

To get more complex sorts, create a sort with a comparator block. Be warned these can create unanticipated draws on memory and cycles so use them carefully.

You can also create a compare function for custom NSManagedObject subclasses that will compare any two objects of the class in anyway you wish. That is usually not necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜