Iphone IOS 5核心数据,如何让NSFetchedResultsController返回与父一对多关系匹配的结果?
我正试图找到一种最简单的方法,用核心数据展示一组有限的托管对象 我在核心数据中定义了一个用户对多个事件的关系 我需要能够让我的UITableViewController一次为一个用户显示结果,同时保留为该用户插入/删除事件的能力。如果我只需要显示数据,我会将NSSet转换为tableview数据源我想保留添加/删除和编辑事件的功能,但仅限于当前活动用户。 每次需要更改用户时,我都会将NSFetchedResultsController设置为nil,然后像下面那样重新初始化它。这保留了处理数据的能力,但需要一些属性来过滤数据 目前我正在考虑这样实现:为用户定义一个ID属性,然后为用户的所有事件添加相同的ID。这将允许我使用Iphone IOS 5核心数据,如何让NSFetchedResultsController返回与父一对多关系匹配的结果?,iphone,objective-c,ios,core-data,Iphone,Objective C,Ios,Core Data,我正试图找到一种最简单的方法,用核心数据展示一组有限的托管对象 我在核心数据中定义了一个用户对多个事件的关系 我需要能够让我的UITableViewController一次为一个用户显示结果,同时保留为该用户插入/删除事件的能力。如果我只需要显示数据,我会将NSSet转换为tableview数据源我想保留添加/删除和编辑事件的功能,但仅限于当前活动用户。 每次需要更改用户时,我都会将NSFetchedResultsController设置为nil,然后像下面那样重新初始化它。这保留了处理数据的能
NSPredicate
搜索用户的事件,如下定义
是否有其他方法可以使用核心数据关系解决我的问题?我觉得一定有,但我看不见。
以下是我用于搜索的代码:我在显示此NSFetchedResultsController
以获得部分结果和常规NSFetchedResultsController
以获得完整结果之间进行切换
- (NSFetchedResultsController *)searchFetchedResultsController
{
if (__searchFetchedResultsController != nil) {
return __searchFetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// NSString *attributeName = @"type";
// // NSString *attributeValue = [NSString stringWithFormat:@"%i",dateID];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %i",
// attributeName, 0];
// [fetchRequest setPredicate:predicate];
//
// NSString *attributeName = @"type";
// // NSString *attributeValue = [NSString stringWithFormat:@"%i",dateID];
NSPredicate *notesPredicate = [NSPredicate predicateWithFormat:@"notes CONTAINS[cd]%@", self.searchString];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd]%@", self.searchString];
NSPredicate* compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:notesPredicate,namePredicate, nil]];
[fetchRequest setPredicate:compoundPredicate];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
#warning cache may prevent search results from updating
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"dayID" cacheName:@"SearchResults"];
self.searchFetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.searchFetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
// abort();
}
NSLog(@"found objects: %i",[__searchFetchedResultsController.fetchedObjects count]);
return __searchFetchedResultsController;
}
谢谢你的帮助 无需在事件实体中创建ID字段来标识用户。您说您已经有了关系设置用户->事件。关系是双向的(假设您以这种方式设置关系,它被称为双向的,并且始终使用它们是最佳实践),因此您可以轻松创建一个NSPredicate,用于查找user=user的事件。无需在事件实体中创建ID字段来标识用户。您说您已经有了关系设置用户->事件。关系是双向的(假设您以这种方式设置它,它被称为双向的,并且始终使用它们是最佳实践),因此您可以轻松创建一个NSPredicate,以查找user=user的事件。您将
NSFetchedResultsController设置为nil
的方法非常可靠。获取的结果控制器应该延迟创建,如果需要更改谓词,只需将其设置为nil
确实不需要您自己的id字段。在事件的获取请求中,只需做如下谓词:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"user = %@", userObject];
其中“user”是您在事件实体中为关系指定的名称。您将nsfetchedresultscoontroller
设置为nil
的方法非常可靠。获取的结果控制器应该延迟创建,如果需要更改谓词,只需将其设置为nil
确实不需要您自己的id字段。在事件的获取请求中,只需做如下谓词:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"user = %@", userObject];
其中“user”将是您在事件实体中为关系指定的名称。谢谢您的回答,这正是我需要的!谢谢你的回答,这正是我需要的!