开发者

Confusion with NSFetchedResultsController

Hi I have a evolving core data model setup as follows:

lookup -> detail -> itemDetail

lookup is at the top level and for every 1 entity there can many in detail and for every entity in detail there can be 1 in itemDetail.

Prior to implementing itemDetail within my core data "engine" I have a method as follows:

`- (NSFetchedResultsController *)fetchedResultsController {

// if controller already created and the caller does not need a new one...

if (fetchedResultsController != nil && !needsNewFetchedResultsController) {

    return fetchedResultsController;
}


// we must flush the class cache first

[NSFetchedResultsController deleteCacheWithName:@"searches"];


// then set to nil to remove existing data

self.fetchedResultsController = nil;


// resetflag back to NO for future calls into this method

self.needsNewFetchedResultsController = NO;


// all the search results are in the "SearchDetail" table and categorised by the resource type

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"resource_type == %@ AND lookup.search_phrase == %@", self.resource, self.searchText];


// create the fetch request and set the predicate

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

fetchRequest.predicate = predicate;


// hook up to the required table  

NSEntityDescription *entity = [NSEntityDescription entityForName:@"SearchDetail" inManagedObjectContext:self.context];

[fetchRequest setEntity:entity];


// NSFetchedRequestController wants a sort, but we dont so just use type field to placate it ;-)

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"resource_type"开发者_开发技巧 ascending:NO];

[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                                 managedObjectContext:self.context 
                                                                                                   sectionNameKeyPath:nil 
                                                                                                            cacheName:@"searches"];

self.fetchedResultsController = theFetchedResultsController;


self.fetchedResultsController.delegate = self;

[theFetchedResultsController release];

[sort release];

[fetchRequest release];

return self.fetchedResultsController;    

} `

This allows me to retrieve data from detail for a search performed on lookup and all works well.

But now that I have included itemDetail I now need to grab data from itemDetail when a entry in detail is selected......but NSFetchedResulstController in my core data engine is setup up as above for a different query?

Do I need to have multiple methods for different NSFetchedResultsController or do I need to do some form of flag checking in existing one?

As my model grows I can see this growing into a beast of a problem so need to undertand it early rather than too late!

In hope....


I am not sure I understand correctly your model. Assuming that your model contains the entities lookup, detail and itemDetail, the proper way to model this situations is as follows:

lookup contains a to-many relationship to detail, call it details; detail contains a to-one relationship to itemDetail, call it itemDetail;

Now, when you select a lookup object, you do not need any additional query (and therefore no additional NSFetchedResultsController) to retrieve the detail objects and from a detail object its corresponding itemDetail.

You can simply do this:

NSSet *details = yourSelectedLookupObject.details;

// now browse the detail objects
for(Detail *detailObject in details){
    // get the associated itemDetail
    ItemDetail *itemDetail = detailObject.itemDetail;

    // process itemDetail as needed ...

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜