Iphone NSSORTD对许多关系的描述
我使用核心数据数据库。 我有实体相册,它有许多实体特定图像(获取这些图像集的关键是@“特定图像”) 我想获得id为1的相册,并按id对附加的特定图像进行排序。 我试过了 但是我得到了错误 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“此处不允许使用多个密钥” 这是我的密码:Iphone NSSORTD对许多关系的描述,iphone,objective-c,one-to-many,nssortdescriptor,Iphone,Objective C,One To Many,Nssortdescriptor,我使用核心数据数据库。 我有实体相册,它有许多实体特定图像(获取这些图像集的关键是@“特定图像”) 我想获得id为1的相册,并按id对附加的特定图像进行排序。 我试过了 但是我得到了错误 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“此处不允许使用多个密钥” 这是我的密码: NSString *entityName = kEntityName; AppDelegate *appDelegate = [[UIApplication
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 error:nil]时,它抛出一个异常 我假设,如果相册中有许多特定图像,那么从特定图像到相册(特定图像中有一个相册对象)的关系也是相反的 在这里,我首先查找所需唱片集id的所有特定图像,而不是所有唱片集,最后从其中一个特定图像对象中获取唱片集对象
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数组,album是您所需的唱片集,id=CurrentCategoryItemID。我假设,如果唱片集中有许多specificImages,则specificImages与唱片集之间的关系也是相反的(SpecificImage1个唱片集对象) 在这里,我首先查找所需唱片集id的所有特定图像,而不是所有唱片集,最后从其中一个特定图像对象中获取唱片集对象
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是您正在查找的特定图像的数组,相册是您所需的相册,id=CurrentCategoryItemID。我认为,没有任何变体,如何做到这一点。我有这个问题。但在执行fetch请求后,我必须按id对其排序。我认为,没有任何变体,如何做到这一点。我有这个问题。但在执行fetch请求后,我必须按id对其进行排序。我想对附加的图像进行排序,但不想对图像进行排序,然后返回它们的相册。所以我应该使用类似于[NSSortDescriptor sortdescriptor with key:@“specificmages.id”升序:YES]的东西;specificimages是nsset对象。。所以里面没有身份证。。在fetchRequest中添加一个sortdescriptor,通过比较所有结果对象中th键的值对结果数组进行排序..我所做的是解决您的问题的最佳方案。。还有一个效率较低的解决方案。。这将不包括fetchRequest中的任何SortDescriptor。。或者,您获取所需的相册,然后访问其SpecificAges nsset并将值排序为ur Desired。我希望对附加的图像进行排序,但不希望对图像进行排序,然后返回相册。所以我应该使用类似于[NSSortDescriptor sortdescriptor with key:@“specificmages.id”升序:YES]的东西;specificimages是nsset对象。。所以里面没有身份证。。在fetchRequest中添加一个sortdescriptor,通过比较所有结果对象中th键的值对结果数组进行排序..我所做的是解决您的问题的最佳方案。。还有一个效率较低的解决方案。。这将不包括fetchRequest中的任何SortDescriptor。。或者,您获取所需的相册,然后访问其SpecificAges nsset,并根据您的需要对值进行排序