我使用核心数据数据库。我有实体相册,它有许多实体SpecificImage (获取这些图像设置的键是@" SpecificImages "),我希望获得id =1的相册,并按id对附加的SpecificImages进行排序。我试过了
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"specificImages.id" ascending:YES];但我明白错误
由于
异常“NSInvalidArgumentException”终止应用程序,原因:“to -多键在此不允许”
这是我的代码:
NSString *entityName = kEntityName;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];;
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSEntityDescription *entityDesctiption = [NSEntityDescription
entityForName: entityName
inManagedObjectContext:context];
// find object
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %d", CurrentCategoryItemID];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"specificImages.id" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[request setEntity:entityDesctiption];
[request setPredicate:predicate];
NSArray *objects = [context executeFetchRequest:request error:nil];
[request release];
if (objects == nil)
{
NSLog(@"there was an error");
return;
}
else if ([objects count] == 0)
{
return;
}
NSManagedObject *object = [objects objectAtIndex:0];当我尝试执行NSArray *objects = context executeFetchRequest:request :nil时,它会引发一个异常;
发布于 2012-07-09 06:15:38
我假设,如果相册中有许多SpecificImages,那么从SpecificImage到相册( SpecificImage中的一个相册对象)也会有一个相反的关系。
在这里,我首先为所需的相册id找到所有的SpecificImages,而不是所有的相册,最终形成一个SpecificImage对象,我将得到相册对象。
NSString *entityName = @"SpecificImage";
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];;
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSEntityDescription *entityDesctiption = [NSEntityDescription
entityForName: entityName
inManagedObjectContext:context];
// find object
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album.id == %d", CurrentCategoryItemID];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[request setEntity:entityDesctiption];
[request setPredicate:predicate];
NSArray *specificImagesArray = [context executeFetchRequest:request error:nil];
[request release];
if (objects == nil)
{
NSLog(@"there was an error");
return;
}
else if ([objects count] == 0)
{
return;
}
Album *album = [(SpecificImage *)[specificImagesArray objectAtIndex:0] album];specificImagesArray是您正在寻找的specificImages数组,相册是您所需的id = CurrentCategoryItemID相册。
发布于 2012-07-13 12:48:56
我认为,没有任何变体,如何做到这一点。我有这个问题。但是在执行了fetch请求之后,我不得不按id对其排序。
https://stackoverflow.com/questions/10813826
复制相似问题