问题
I use Core Data database. I have Entity Album, that has many entities SpecificImage (key to get set of these images is @"specificImages") I want to get Album with id = 1 and to sort attached SpecificImages by id. I tried
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"specificImages.id" ascending:YES];
but I get the error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
Here is my code:
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];
it throws an exception, when I try to execute NSArray *objects = [context executeFetchRequest:request error:nil];
回答1:
I am assuming, if you have many SpecificImages in Album then you will also have an inverse relationship from SpecificImage to Album ( 1 Album object in SpecificImage ).
Here I am first finding all SpecificImages for the required album id rather than all Albums and in the end form one of those SpecificImage object I will get the album object back.
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];
The specificImagesArray is the array of specificImages you are looking for and album is your required album with id = CurrentCategoryItemID.
回答2:
I think, there is no any variant, how to do this. I had this problem. But I had to sord it by id after executing fetch request.
来源:https://stackoverflow.com/questions/10813826/nssortdescriptor-to-many-relationships