我有一个Entity
在CoreData
模仿MySQL
数据库表结构如下:SQL语句和单一核心数据获取请求
Photo
abv Double
photoId Integer16
photographer String
isActive Boolean
lastUpdated Data
name String
我可以运行以下SQL
声明让我期望的结果集:
SELECT `photographerName`, COUNT(`photographerName`)
FROM `Photos`
WHERE `isActive` LIKE 1
GROUP BY `photographerName`
ORDER BY `photographerName`
什么的NSFetchRequest
组合,NSSortDescriptor
,NSPredicate
,我可以用iOS
达到相同的结果吗?
我使用了以下内容:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
NSSortDescriptor *photographerSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"photographer" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSPredicate *onlyActivePhotos = [NSPredicate predicateWithFormat:@"isActive == 1"];
request.sortDescriptors = @[photographerSortDescriptor];
request.predicate = onlyActivePhotos;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
这让我的isActive
罚款,但返回一行,每Photo
,不Photographer
。
或...我应该把它分成两个Entities
?例如,Photographer
与Photos
有一对多的关系?
'photographerName' - >'photographer'? – paulmelnikow