如何每次使用coredata从表中获取10条记录

Pre*_*oti 2 core-data ios swift

我想从数据库中获取10条记录。一旦获取了前10条记录,下一次我要使用coredata从数据库中获取另外10条记录。

还希望处理类似这样的情况-如果表中的记录少于10条,则在最后获取一些记录后如何处理该记录。

小智 8

为了处理分页,我正在考虑您正在正确获取数据,

在发出 fetch 请求时,请执行此操作并从您的代码动态处理偏移量,

// for 1st time first request
request.fetchOffset = 0;
request.fetchLimit = 10;

// for 2nd time second request
request.fetchOffset = 10;
request.fetchLimit = 10;
Run Code Online (Sandbox Code Playgroud)


Muk*_*esh 6

我认为答案足以满足您的理解,我只是上传工作用例,并确保fetchOffSet对于第一个请求应为0,然后根据您的要求进行动态处理

最初声明并初始化 NSInteger fetchOffSet = 0;

目标C

-(NSMutableArray *)getCountryFromDB:(NSInteger)fetchOffSet {

NSMutableArray *_record = [[NSMutableArray alloc] initWithCapacity:0];

   NSManagedObjectContext *_context =[self getManagedObjectContext];
   NSFetchRequest *_fetchRequest = [[NSFetchRequest alloc]init];
    _fetchRequest.fetchLimit = 10;
   _fetchRequest.fetchOffset = fetchOffSet;

   NSEntityDescription *_entityDesc =[NSEntityDescription entityForName:@"Country" inManagedObjectContext:_context];
    [_fetchRequest setEntity:_entityDesc];

    NSError *_error;
    NSArray *_fetchedOjects = [_context executeFetchRequest:_fetchRequest error:&_error];

   for(int i=0;i<[_fetchedOjects count];i++) {
       Country *_country = [_fetchedOjects objectAtIndex:i];
       [_record addObject:_country];
    }
   return _record;
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

// UITableView only moves in one direction, y axis
CGFloat currentOffset = scrollView.contentOffset.y;
CGFloat maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

// Change 50.0 to adjust the distance from bottom
if (maximumOffset - currentOffset <= 50.0) {
     if(_yourCoreDataRecordArray.count > 10){
             fetchOffSet = fetchOffSet + 10;
            NSMutableArray *array = [self getCountryFromDB:fetchOffSet];

         }        
    }
}
Run Code Online (Sandbox Code Playgroud)

迅速

func getCountryFromDB(_ fetchOffSet: Int) -> [Any] {
    var record = [Any]() /* capacity: 0 */
    var context: NSManagedObjectContext? = self.getManagedObjectContext()
    var fetchRequest = NSFetchRequest()
    self.fetchRequest.fetchLimit = 10
    self.fetchRequest.fetchOffset = fetchOffSet
    var entityDesc = NSEntityDescription.entity(forEntityName: "Country", in: self.context)
    self.fetchRequest.entity = self.entityDesc
    var error: Error?
    var fetchedOjects: [Any]? = try? self.context.fetch(self.fetchRequest)
    for i in 0..<self.fetchedOjects.count {
        var country: Country? = self.fetchedOjects[i]
        self.record.append(self.country)
    }
    return self.record
}

 func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        // UITableView only moves in one direction, y axis
    var currentOffset: CGFloat = scrollView.contentOffset.y
    var maximumOffset: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height
    // Change 50.0 to adjust the distance from bottom
    if maximumOffset - currentOffset <= 50.0 {
        if self.yourCoreDataRecordArray.count > 10 {
            fetchOffSet = fetchOffSet + 10
            var array: [Any] = self.getCountryFromDB(fetchOffSet)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)