Sort NSFetchedResultsController with an object in an NSSet owned by an entity

I have an object with a name Deliverythat has a set of objects associated with it Customer. Each delivery object also stores mainCustomerIdwhich is NSNumber*. I have an NSFetchedResultsController that is used to control the data source for UITableView. The problem is that I want to sort the NSFetchedResultsController according to the client’s lastName field (the client is again stored in the many-to-many relationship called customersin the object Delivery), where one of the clients in the set has customerId conforming to the Delivery MainCustomerId standard.

Delivery class looks like this (only relevant parts)

@interface Delivery : NSManagedObject
@property (nonatomic, retain) NSNumber * mainCustomerId;
@property (nonatomic, retain) NSSet *customers;
@end

The client class is as follows

@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

NSSortDescriptor, - (, , . , )

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"customers.AnyobjectInSet.lastName WHERE AnyobjectInSet.customerId == masterCustomerId ascending:YES];

, NSExpressions, , - , Cocoa (, @selector), mysql no Cocoa . , - , mysql.

select * from Delivery JOIN Customer ON Customer.customerId=Delivery.mainCustomerId ORDER BY Customer.lastName;

( , , , . ). .

.

+5
1

, , - , . , , , ( , mainCustomerID):

NSNumber *mainCustomerID = ...

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"customerID == %@", mainCustomerID];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Customer"];
[request setPredicate:predicate];
[request setSortDescriptors:@[ sortDescriptor ]];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request ...

, , , , mainCustomerID, lastName. WILL SQL, .

, ?

, SQL, CoreData ( ), "" ", ":

-com.apple.CoreData.SQLDebug 1

:)

+2

All Articles