我有一个名为Delivery的Customer对象,它有一组与之关联的对象.每个Delivery对象还存储mainCustomerId一个NSNumber*.我有一个NSFetchedResultsController,用于管理a的数据源UITableView.问题是我想通过Customer的lastName字段对NSFetchedResultsController进行排序(客户再次存储在对象customers上调用的多对多关系中Delivery),其中集合中的一个客户的customerId等于Delivery的MainCustomerId .
交货类看起来像这样(只有相关部分)
@interface Delivery : NSManagedObject
@property (nonatomic, retain) NSNumber * mainCustomerId;
@property (nonatomic, retain) NSSet *customers;
@end
Run Code Online (Sandbox Code Playgroud)
客户类看起来像这样
@interface Customer : NSManagedObject
@property (nonatomic, retain) NSNumber * customerId;
@property (nonatomic, retain) NSString * lastName;
// And the inverse relationship to the deliveries
@property (nonatomic, retain) NSSet *deliveries;
@end
Run Code Online (Sandbox Code Playgroud)
我需要创建一个类似这样的NSSortDescriptor(注意,我知道这是错误的格式并且不起作用.我希望它能传达这个想法)
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"customers.AnyobjectInSet.lastName WHERE AnyobjectInSet.customerId == masterCustomerId ascending:YES];
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几个使用子查询和NSExpressions的东西,但总是很短,因为我不能使用任何使用Cocoa的功能(比如使用@selector进行排序),因为它必须能够生成一个真正的mysql查询而没有Cocoa处理数据.但我觉得必须要做到这一点,因为它在mysql中是一个简单的查询.
select * from …Run Code Online (Sandbox Code Playgroud) objective-c nssortdescriptor nsset nsfetchedresultscontroller ios