0
在這裏你可以看到項目和翻譯之間的關係。使用多對多關係排序fetchRequest(核心數據)?
我想什麼是使用特定Translation.language通過Translation.name項目進行排序。結果應該是一個有序數組,其中的項目按特定語言排序,例如英語,德語等。
THX
在這裏你可以看到項目和翻譯之間的關係。使用多對多關係排序fetchRequest(核心數據)?
我想什麼是使用特定Translation.language通過Translation.name項目進行排序。結果應該是一個有序數組,其中的項目按特定語言排序,例如英語,德語等。
THX
編輯
所有你需要做的是用一種描述符,以獲取所有項目的翻譯
我使用一個功能我在過去寫道:
+(NSArray*)fetchForEntity:(NSString*)entityName withPredicate:(NSPredicate*)predicate withSortDiscriptor:(NSString*)sortdDscriptorName{
NSManagedObjectContext *moc=[[[UIApplication sharedApplication] delegate]managedObjectContext];
NSEntityDescription *entityDescription;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc];
[request setEntity:entityDescription];
[request setPredicate:predicate];
if (sortdDscriptorName) {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:sortdDscriptorName ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}
NSError *error = nil;
NSArray * requestArray =[moc executeFetchRequest:request error:&error];
if (requestArray == nil)
{
// Deal with error...
}
return requestArray;
}
在你的情況下,你應該這樣稱呼它:
NSString *languageName = @"German"; //or what ever
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"language == %@",languageName];
NSArray *array = [self fetchForEntity:@"Translation" withPredicate:predicate withSortDiscriptor:@"name"];
現在您有一個德語版的所有翻譯列表。 然後你就可以得到你需要與所有項目:
NSMutableArray *itemsArray = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Translation *translation = (Translation*)obj;
Item *item = translation.item;
[itemsArray addObject:item];
}];
希望它可以幫助 沙尼
感謝您的快速響應,但不起作用。我得到這個錯誤:'由於未捕獲的異常'NSInvalidArgumentException'終止應用程序,原因:'對多密鑰不允許在這裏'。這是因爲翻譯(對多)關係。 – MoFuRo
對不起,我的錯誤。我會更新我的衣服。 – shannoga
我也忘了說我想按translations.name排序。在你的回答中你寫了translations.language,但這不應該有很大的不同。 – MoFuRo