其实分页查询我第一反映是用SQLite的查询语句来操作,后来想想Core Data底层也是调用SQLite,应该也是可以分页查询的
不过iPhone开发毕竟中文资料不多,翻了下官方英文文档,找到下面的方法,主要使用下面两个函数
// 限定查询结果的数量 setFetchLimit // 查询的偏移量 setFetchOffset
最近正在熟悉各种API,没太多时间,直接上代码,其实蛮简单的
NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"RSSEntryModel" inManagedObjectContext:_managedObjectContext]; [request setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"articleDate" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; [request setFetchLimit:21]; [request setFetchOffset:_currentPage * 21]; NSArray *rssTemp = [_managedObjectContext executeFetchRequest:request error:&error];
这里贴上一个教程http://www.raywenderlich.com/934/core-data-tutorial-getting-started,3个篇幅说得更加仔细
他blog里的教程都很棒,我基本都看了遍
