小编joh*_*chd的帖子

使用属于实体的NSSet中的对象对NSFetchedResultsController进行排序

我有一个名为DeliveryCustomer对象,它有一组与之关联的对象.每个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查询而没有C​​ocoa处理数据.但我觉得必须要做到这一点,因为它在mysql中是一个简单的查询.

select * from …
Run Code Online (Sandbox Code Playgroud)

objective-c nssortdescriptor nsset nsfetchedresultscontroller ios

5
推荐指数
1
解决办法
740
查看次数