正如我在亞當埃伯巴赫的答案的評論說,我解決了這個使用NSDictionary
(self.workOrdersByDate
,下同)來存儲分組結果,以及一個NSArray
(self.dateSections
,下同)存儲字典的排序鍵。
// Fetch all the WorkOrders
NSArray *results = [WorkOrder findAll];
// Filter based on isComplete property
NSPredicate *filter;
if (self.segmentedStatus.selectedSegmentIndex == SegmentAssigned) {
filter = [NSPredicate predicateWithFormat:@"isComplete == 0"];
} else {
filter = [NSPredicate predicateWithFormat:@"isComplete == 1"];
}
results = [results filteredArrayUsingPredicate:filter];
// Retain an NSArray of sorted NSDate keys
self.dateSections = [[results valueForKeyPath:@"@distinctUnionOfObjects.scheduledStartDate.beginningOfDay"] sortedArrayUsingSelector:@selector(compare:)];
// Create the dictionary
self.workOrdersByDate = [[NSMutableDictionary alloc] initWithCapacity:self.dateSections.count];
for (int sectionNum = 0; sectionNum < self.dateSections.count; sectionNum++) {
NSPredicate *sectionPredicate = [NSPredicate predicateWithFormat:@"scheduledStartDate.beginningOfDay == %@", [self.dateSections objectAtIndex:sectionNum]];
NSArray *workOrdersForSection = [results filteredArrayUsingPredicate:sectionPredicate];
[resultsDict setObject:workOrdersForSection forKey:[self.dateSections objectAtIndex:sectionNum]];
}
正確的我可以做到這一點,但後來我失去了所有使用NSFetchedResultsController免費獲得的排序和分組。儘管如此,這可能是我的最佳選擇。 –
除非該布爾值可以添加到NSManagedObject我不知道更好的方法。 –
我最終完全拋棄了'NSFetchedResultsController',並使用'NSDictionary'實現這個以保留NSFetchedResultsController給我的分組。我將字典的已排序鍵保存在單獨的數組中。 –