iOS CoreData排序之 NSFetchRequest

http://www.cnblogs.com/karling/p/5033525.html
https://blog.csdn.net/a724765518/article/details/41893803
http://www.cnblogs.com/fortunely/p/4725982.html

不写代码了,补充几点:

  • 不使用任何排序的话,默认按照先入先出顺序,最早录入的信息在查询数组最开头,如果想倒叙的话,把数组反向遍历即可
  • 并不通过条件类 NSPredicate 来排序
  • 使用单独的 NSFetchRequest 来排序,排序使用的是创建的内容比较
  • coredata 没有自动id, 所以不需要用order by id这样的想法来排序

根据时间排序

数据表key设置为 NSDate 类型即可

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:yourContext];
NSPredicate *beginningPredicate = [NSPredicate predicateWithFormat:@"createdAt >= %@", beginningTime];
NSPredicate *endPredicate = [NSPredicate predicateWithFormat:@"createdAt <= %@", endTime];
request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:beginningPredicate, endPredicate, nil]];
NSArray *results = [yourContext executeFetchRequest:request error:NULL];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容