如何使用coredata每次从表中获取10条记录 [英] How fetch 10 records each time from table using coredata

查看:90
本文介绍了如何使用coredata每次从表中获取10条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to fetch 10 records from database. once the first 10 records are fetched, next time I want another 10 record from the database using coredata.

也希望处理类似的情况-在如果表中的记录少于10条,则结束该如何处理。

Also want to handle the condition like - After fetching some records at the end if there is less than 10 record in the table how to handle this.

推荐答案

我认为很多答案足以使您理解,我只是在上载我的工作案例&确保第一个请求的fetchOffSet应该为0&然后根据您的要求动态

I think that much answer is enough for your understanding,I am just uploading my working case & make sure fetchOffSet should 0 for 1st request & then dynamic as per your requirement,

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

Initially declare & initialize NSInteger fetchOffSet = 0;

Objective-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];

         }        
    }
}

迅速

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)
        }
    }
}

这篇关于如何使用coredata每次从表中获取10条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆