开发者

Use a set from a managed object as data for a fetched results controller

I want to make a Navigation interface, where a user can tap on a cell and have a new navigation controller the same as the previous one come up. My managed objects have the following structure:

name (string)
orderId (int)
orderBy (string, a key path indicating what to order the table with)
dateCreated (date)
items (a relationship pointing t开发者_StackOverflow社区o the items for the next table)

On tapping an item with a non-nil items, the next controller get's a reference to the tapped item and use its "items", "orderBy" and "orderId" to construct a fetched results controller (with its items as data) and a sort descriptor (using the orderBy and OrderId).

How can I tell the fetched results controller to use the NSSet returned by items as its data? Can I use a predicate to limit results to the items of just one object? Thanks


Your items need to have a reverse many-to-one relationship. Lets call that relationship "parent".

In the didSelectRowAtIndexPath: of your tableView delegate, initiate a new view controller, and pass the "parent" object at said indexPath. You will need to create an accessor to hold said object in the next view controller.

As you properly guessed, you can make a predicate so that your fetchedResultsController only returns said "items". Make your fetched results only search for the "item" entity.

NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"parent == %@", parentObject];
[fetchRequest setPredicate:resultsPredicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
    initWithKey:[parentObject valueForKey:@"orderBy"] ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

Basically, you are searching for all of the "items" that have a parent relationship to the object you selected.

Let me know if I described it properly (sorry if I didn't, but I do this myself, and can point to an Apple example).

EDIT: Apple Sample Code

http://developer.apple.com/library/ios/#samplecode/CoreDataBooks/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008405

http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008913

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜