与NSFetchedResultsController混淆

时间:2011-08-05 09:29:37

标签: iphone core-data nsfetchedresultscontroller

您好我有一个不断发展的核心数据模型设置如下:

查找 - >细节 - > itemDetail

查找位于顶层,每1个实体可以有很多详细信息,每个实体都可以在itemDetail中有1个。

在我的核心数据“引擎”中实现itemDetail之前,我有一个方法如下:

` - (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;    

} `

这允许我从查找的搜索的详细信息中检索数据并且一切正常。

但是现在我已经包含了itemDetail,我现在需要在选择详细条目时从itemDetail获取数据......但我的核心数据引擎中的NSFetchedResulstController是如上设置的,用于不同的查询?

我是否需要为不同的NSFetchedResultsController提供多种方法,还是需要在现有的方法中进行某种形式的标记检查?

随着我的模型的增长,我可以看到它成长为一个问题的野兽所以需要尽早而不是太迟!

希望......

1 个答案:

答案 0 :(得分:1)

我不确定我的模型是否正确。假设您的模型包含实体lookup,detail和itemDetail,建模这种情况的正确方法如下:

查找包含与详细信息的多对多关系,称之为详细信息; detail包含与itemDetail的一对一关系,称之为itemDetail;

现在,当您选择查找对象时,您不需要任何其他查询(因此没有额外的NSFetchedResultsController)来检索详细信息对象,也不需要从详细对象中检索相应的itemDetail。

你可以这样做:

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 ...

}