通过SQL加入和分组到NSFetchRequest语句

时间:2016-08-02 07:37:50

标签: ios core-data

我第一次使用核心数据,在从两个Tables / Entity中获取数据时遇到问题。

SQL如下所示:

Select * from ZMESSAGE msg Left Join ZCONTACT c ON c.ZCHANNEL = msg.ZCHANNEL group by msg.ZCHANNEL Order by msg.ZDATE 

您能否建议,如何使用NSFetchRequest语句编写?

我的实体如下: enter image description here

我尝试了一些案例,最后一个案例如下:

NSFetchRequest* fetchGroupSummary = [NSFetchRequest fetchRequestWithEntityName:@"Message"];

NSEntityDescription* entity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:self.managedObjectContext];

NSRelationshipDescription *groupName = [entity.relationshipsByName objectForKey:@"channels"];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"channels.channel == channel"];

[fetchGroupSummary setEntity:entity];
[fetchGroupSummary setSortDescriptors:@[sortDescriptor]];
[fetchGroupSummary setPropertiesToFetch:[NSArray arrayWithObjects:groupName, nil]];
[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:groupName]];
[fetchGroupSummary setResultType:NSDictionaryResultType];
[fetchGroupSummary setPredicate:predicate];

NSError* error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchGroupSummary error:&error];

return results;

由于

0 个答案:

没有答案