iPhone coredata 获取请求,关系和部分的基础知识
我有一个希望是一个简单的问题 - 我有两个实体 - List 和 ListItem - 并且它们之间建立了一对多关系,一切都很好。
当我尝试执行 fetchrequest 时,我的问题出现了,该请求将返回按列表项的属性划分的列表项。我无法直接在 listItem 对象上执行 fetchrequest,因为没有办法说“Where List = 'xxxxx'”,所以我有类似以下内容:
- (NSFetchedResultsController *)getListItems {
// Init a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"List" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"listItem.productName" ascending:YES selector:nil];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:descriptors];
// Init the fetched results controller
NSError *error;
self.globalFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"listItem.productName" cacheName:@"listItems"];
self.globalFetchedResultsController.delegate = self;
if (![[self globalFetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);
[fetchRequest release];
[sortDescriptor release];
return self.globalFetchedResultsController;
}
现在,这个错误是因为它说我不能使用一个 -排序描述符中的对多关系 - 但它也要求我有一个排序描述符,所以我不确定执行此操作的正确方法。任何帮助都会很棒。
I've got what is hopefully a simple question - I've got two entities - List and ListItem - and there's a one-to-many relationship set up between them, all good.
My problem comes when I'm trying to perform a fetchrequest which will return the listitems sectioned up by an attribute of the listitem. I can't perform a fetchrequest directly on the listItem objects because there's no way to say 'Where List = 'xxxxx'', so I had something like the following:
- (NSFetchedResultsController *)getListItems {
// Init a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"List" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"listItem.productName" ascending:YES selector:nil];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptors:descriptors];
// Init the fetched results controller
NSError *error;
self.globalFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"listItem.productName" cacheName:@"listItems"];
self.globalFetchedResultsController.delegate = self;
if (![[self globalFetchedResultsController] performFetch:&error]) NSLog(@"Error: %@", [error localizedDescription]);
[fetchRequest release];
[sortDescriptor release];
return self.globalFetchedResultsController;
}
Now, this errors because it's saying I can't use a one-to-many relationship in the sort descriptor - but it also requires that I have a sort descriptor, so I'm not sure the correct way to be doing this. Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想获取并分段 ListItems,那么您应该使用“ListItem”作为获取的实体。
在这种情况下,有一种说法是“获取所有 List = 'xxxxx' 的 ListItem 对象”,它称为谓词(NSPredicate)。
尝试这样的操作(其中“list”是 ListItem 与 List 的关系的名称):
另外,请参阅 谓词编程指南。
If you want to fetch and section ListItems, then you should be using "ListItem" as your entity for the fetch.
In this case, there is a way to say "fetch all ListItem objects Where List = 'xxxxx'", it's called a predicate (NSPredicate).
Try something like this (where "list" is the name of the ListItem relationship with List):
Also, see the Predicate Programming Guide.