繁体   English   中英

一对多关系作为sectionNameKeyPath

[英]to-many relation as sectionNameKeyPath

我想显示Project,使其在sharedToUsers基础上显示所有项目。 用User.user_id表示节名称,它应该显示与该用户共享的所有项目。 但是我无法正确设置NSFetchedResultsController的 section值,因为它关系太多,并且在sectionNameKeyPath崩溃 任何帮助

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *theParent = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:_managedObjectContext];
//the name key exsit in parent entity of project.
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:10];
[fetchRequest setEntity:theParent];

[fetchRequest setPredicate:_predicate];

  NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:_managedObjectContext sectionNameKeyPath:@"ANY sharedToUsers.user_id"
                                               cacheName:[self getCacheNameForNSFetchedResultsController]];

核心数据中的多对多关系

如果您有像我一样的年龄,那么无论何时更改核心数据中的数据,都不要尝试使用父对象或类别对象来添加或更新关系船项目。 如果必须将某些东西与类别链接,则尝试从子对象进行链接。

不要使用这个

[category addRelationShipItem:child];

用这个

`[child addToCategory:category];`

当您使用它时,它将不会触发NSFetchResultController调用中的更新调用。

快乐的编码。

嗨,如果您想获取用户共享的项目,则需要以下谓词:

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *theParent = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:_managedObjectContext];

   NSString *userId; // If you user_id is a NSNumber change you to it.
   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY sharedToUsers.user_id = %@",userId];
   [fetchRequest setPredicate:predicate];

最后,您正在使用NSSort:name,但命名它不是Project属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM