0

特定のカテゴリに関連付けられているすべての Core Data から呼び出そうとしています。アプリは次のようなものです。

  • カテゴリーをクリック
  • サブカテゴリの質問をクリックします
  • 質問を見る

ビューをすべてセットアップし、パートナーに Core Data をセットアップしてもらいましたが、選択したカテゴリに関係なく、常にすべての質問が読み込まれるという問題に直面しています。

カテゴリ リスト ビューからカテゴリ選択を渡しましたが、どうすればいいのか、コア データからどのように呼び出せばよいのかわかりません。私は現在これを持っています(繰り返しますが、すべての質問を返します):NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:[appDelegate managedObjectContext]];

データ モデルでは、カテゴリと質問は逆の関係にあります。述語、NSRelationshipDescription、または何か他のものを使用する必要がありますか?

4

2 に答える 2

0

使用NSPredicate(従来のマスター/ディテールUITableViewパターンとストーリーボードを使用していると仮定):

// In CategoryViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"categorySelect"])
    {
        Category *category;
        category = [categories objectAtIndex:[self.tableView indexPathForSelectedRow].row];
        [segue.destinationViewController setParentCategory:category];
    }
}

// In QuestionViewController with @property parentCategory
- (void)viewDidLoad
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create predicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category == %@)", self.ParentCategory];
    [fetchRequest setPredicate:predicate];

    NSError *error;
    questions = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
}
于 2013-07-22T04:48:35.900 に答える
0

NSSetの質問にアクセスできませんか?すなわちcategory.questions

述語に関する質問に答えるには:

Questions特定のものCategoryをすべて見つけたい場合はCategoryNSPredicate

何かのようなもの:

(NSArray *)findQuestionsForCategory:(Category *)category {
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:[appDelegate managedObjectContext]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"question.category == %@", category]];

... execute fetch request, handle possible errors ...

}
于 2013-07-22T04:03:12.730 に答える