58934

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

<h3>Question</h3>
<b>Closed</b>. This question needs to be more focused. It is not currently accepting answers. <hr class="my12 outline-none baw0 bb bc-powder-2" /> <svg aria-hidden="true" class="svg-icon iconLightbulb" width="18" height="18" viewbox="0 0 18 18"><path d="M15 6.38A6.48 6.48 0 007.78.04h-.02A6.49 6.49 0 002.05 5.6a6.31 6.31 0 002.39 5.75c.49.39.76.93.76 1.5v.24c0 1.07.89 1.9 1.92 1.9h2.75c1.04 0 1.92-.83 1.92-1.9v-.2c0-.6.26-1.15.7-1.48A6.32 6.32 0 0015 6.37zM4.03 5.85A4.49 4.49 0 018 2.02a4.48 4.48 0 015 4.36 4.3 4.3 0 01-1.72 3.44c-.98.74-1.5 1.9-1.5 3.08v.1H7.2v-.14c0-1.23-.6-2.34-1.53-3.07a4.32 4.32 0 01-1.64-3.94zM10 18a1 1 0 000-2H7a1 1 0 100 2h3z"></path></svg> <p class="mb0"> <b>Want to improve this question?</b> Update the question so it focuses on one problem only by editing this post.

<p class="mb0 mt6">Closed 3 years ago.

</aside>

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.


<h3>Answer1:</h3>

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) } } }
<h3>Answer2:</h3>

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;
<h3>Answer3:</h3>

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

Recommend

  • How fetch 10 records each time from table using coredata [closed]
  • PyQt5 opening second window from main
  • tensorflow manipulate labels vector into “multiple hot encoder”
  • How to copy the dll's using cmake?
  • Using C#, what is the most efficient method of converting a string containing binary data to an arra
  • dijit.byId doesn't work on IE8 with dojo.addOnLoad
  • IE10 Hover + Pseudo Elements
  • Validating link in post-inspector linkedin in gives server error
  • any Python tools for reading Scantron-style data [closed]
  • How to handle URL with special characters present in password in Postman?
  • Jruby and Cucumber / Cucumber Rails - Could not find generator cucumber:install
  • Access to a single pixel in C#
  • Does ADL work for the global namespace?
  • Html Table alignment right
  • why is hash output fixed in length?
  • Jasper reports - Cambodian / Khmer and Laotian Language
  • Ember.js: how to save a model
  • SSRS Count IF Multiple values
  • Can not execute stored procedure JPA
  • C++11 perfect forwarding and reference collapsing
  • Get 0:1(1): error: syntax error, unexpected $end when trying to compile shader
  • Run WinForms control inside WebForms and take screenshot of it
  • MVVM-Light: Load UserControl into Window
  • Display an image using jetty web server from byte array
  • Cocoa: Accepting and responding to keystrokes
  • Matplotlib xlabel in Arial with one word italicised
  • WPF ContextMenu using ItemsControl, incorrectly highlight the whole collection
  • MongoDB : how to set a new field equal to the value of another field, for every document in a collec
  • Highlight special word in a TextBox
  • Holoviews plot not rendered in cell in JupyterLab
  • Write to elasticsearch from spark is very slow
  • SQL Server version 612 , 655?
  • What Exception is thrown on timeout?
  • how to run ejabberd with Erlang on Heroku?