How fetch 10 records each time from table using coredata [closed]

倖福魔咒の 提交于 2020-06-15 19:07:21

问题


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.

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.


回答1:


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,

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

         }        
    }
}

Swift

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



回答2:


For handling pagination i am considering you are fetching data properly,

While giving fetch request do this & handle offset dyanamically from your code,

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

// for 2nd time second request
request.fetchOffset = 10;
request.fetchLimit = 10;



回答3:


just add these two properties into fetchrequest object

nsfetchrequest.fetchLimit = 10
nsfetchrequest.fetchOffset = 10 // this will be changed for every new call 

Let me tell you in detail, the fetchLimit property used to tell the core data the maximum number of objects(record) that a request should return and the fetchOffset skips a given set of results. If you set your offset to 2, then your first two results would not be returned in your results.

For more detail see the documentation:

http://www.learncoredata.com/how-to-fetch-data/

Cheers



来源:https://stackoverflow.com/questions/42112829/how-fetch-10-records-each-time-from-table-using-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!