I have list of messages in my Core Data store(100 messages).
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchLimit:20];
It shows only first 20 messages in the order I need. What I need to do is: Show last 20 messages, sorted by date of creation, but they should appeared form the bottom of the TableView.
Storage:
1 : Test 1
2 : Test 2
3 : Test 3
It shows now as :
1 : Test 1
2 : Test 2
**Supposed to show in table View:**
3 : Test 3
2 : Test 2
1 : Test 1
fetchedResultsController:
- (NSFetchedResultsController *)fetchedResultsController{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Messages" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchLimit:20];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(owner = %@) and (stream == %@) and (topic == %@)", ownerGuid,openStream,currentTopic];
[fetchRequest setPredicate:predicate];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
self.fetchedResultsController.delegate = self;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.fetchedResultsController performFetch:NULL];
return _fetchedResultsController;
}