setFetchBatchSize Fetching all the rows in messages Table

Home / Uncategorized / setFetchBatchSize Fetching all the rows in messages Table

Question:
Following is my code snippet.
NSSortDescriptor *sortSequence = [[NSSortDescriptor alloc]
initWithKey:@"date_added" ascending:YES];
NSArray *sortDescriptors =[NSArray arrayWithObjects:sortSequence,nil];
[fetchRequest setFetchBatchSize:5];
[fetchRequest setSortDescriptors:sortDescriptors];
[NSFetchedResultsController deleteCacheWithName:@“Chats”];
NSFetchedResultsController *theFetchedResultsController =[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext: managedObjectContext sectionNameKeyPath:@"messageSectionKey" cacheName:@"Chats"];

In my listing I want to implement load more functionality. I want to sort based on the key date_added . messageSectionKey is a getter method in the coredata class which will return Yesterday,Today etc based on date_added … But After fetching instead of loading 5, it is loading all the rows in my Message table


Answer:
I guess to get the 5 objects you should set the fetch limit as 5
[fetchRequest setFetchLimit:5];

To get say 20 records , you pass in the parameter value of numberOfItems = 20, do your working inside a function, its just an idea , you can make your own function, I guess you should use only fetchLimit
-(void) getRecordsFor:(NSInteger) numberOfRecords {
….

[fetchRequest setFetchLimit: numberOfRecords];

[fetchRequest setSortDescriptors:sortDescriptors];
[NSFetchedResultsController deleteCacheWithName:@“Chats”];

}
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *