もちろん、Core Data は多対多の関係を完全にサポートしているため、状況を完全に説明していない可能性があります。NSFetchedResultsController は多対多の関係をサポートしていないということでしょうか? 私が判断できる限り、それは正しいです。(編集:多対多の関係で NSFetchedResultsController を使用することは可能です...それを行う方法はあまり明白ではありません。)
NSFetchedResultsController を使用せずにこれを行うには、関心のある A エンティティを識別/フェッチし、関心のある関係をトラバースします。したがって、特定の A オブジェクトに関心があることが既にわかっている場合は、これを theAObject と呼びます。クラス名 A と B の場合、次のようなドット構文と高速列挙を使用して関係をたどることができます。
for (B *theBObject in theAObject.BObjects) {
NSLog(@"theBObject.name: %@.", theBObject.name);
// Run whatever code you want to here on theBObject.
// This code will run once for each B Object associated with theAObject
// through the BObjects relationship.
}
または、関心のある AObjects のセットを取得するフェッチ リクエストを設定し、それぞれの BOjects 関係をトラバースすることもできます。多対多の関係であることには何の違いもありません...各 AObject は、その BObjects 関係にあるすべての B オブジェクトを返します。
後で
、すべての名前を取得してラベルに表示したいとします。あなたのためにそれを分解しましょう:
NSString *myLabel = null;
// You may of course want to be declaring myLabel as a property and @synthesising
// it, but for the sake of a complete example we'll declare it here which means
// it will only have local scope.
for (B *theBObject in theAObject.BObjects) {
myLabel = [myLabel stringByAppendingString:theBObject.name];
// Add a line break in the string after each B object name.
myLabel = [myLabel stringByAppendingString:@"\n"];
}
// Do something with your myLabel string to set your desired label.