我有一個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