簡體   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