我想知道的是如何将NSSortDescriptor
设置为NSFetchRequest
。
我们不能在CoreData
中按比较器进行排序吗?
还有其他工作吗? 有什么建议可以改变我的模型吗?
顺便说一下,我事先不知道我将添加什么类型的recordValue,我将在所有可能的组合中拥有可变数量的recordvalue。我有核心数据模型如图所示,数据存储如下:
(当我想按dateValue排序时,DOB的dataValue只应考虑进行排序)
id stringValue<NSString> dateValue<NSDate> record
name Alex - record1<ZCMORecord>
DOB - 10/10/1990 record1<ZCMORecord>
name Anto - record2<ZCMORecord>
DOB - 05/05/1990 record2<ZCMORecord>
name Max - record3<ZCMORecord>
DOB - 10/10/1990 record3<ZCMORecord>
name Mary - record4<ZCMORecord>
DOB - 01/01/1990 record4<ZCMORecord>
答案 0 :(得分:0)
这是您对NSFetchedResultsController
:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"ZCMORecordValue" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortStringValue = [[NSSortDescriptor alloc]
initWithKey:@"stringValue" ascending:YES];
NSSortDescriptor *sortDate = [[NSSortDescriptor alloc]
initWithKey:@"dateValue" ascending:YES];
[fetchRequest setSortDescriptors:@[sortStringValue, sortDate]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
首先按名称排序,然后按日期排序。如果要按日期排序,再按名称排序,请反转排序描述符的顺序。