1

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;
}
4

2 回答 2

0

您需要做的就是更改要使用的 sortDescriptorascending:NO以便它可以获取最后 20 条消息。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO];

现在,您得到了正确的消息,它们只是在您的阵列中倒退。您可以使用以下代码轻松反转数组。

NSArray *messages = [[[fetchedMessages] reverseObjectEnumerator] allObjects];

编辑:

由于您使用NSFetchedResultsController此解决方案需要大量额外的代码才能工作。

我能想到的最简单的解决方案是实现一个自定义方法,将您的对象从获取的结果控制器中取出。

- (id)objectAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger sectionCount = [self.tableView numberOfRowsInSection:indexPath.section];
    NSInteger newRowIndex = sectionCount - indexPath.row - 1;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:indexPath.section];

    return [self.fetchedResultsController objectAtIndexPath:newIndexPath];
}

然后只需更换

[self.fetchedResultsController objectAtIndexPath:indexPath];

[self objectAtIndexPath:indexPath];
于 2013-07-31T22:17:09.043 回答
0

@Clever 答案对我有用。我只打电话NSIndexPath(返回newIndexPath)。我没有覆盖objectAtIndexPath. 我被替换为下面的方法。

- (NSIndexPath *)objectNewIndexPath:(NSIndexPath *)indexPath {

NSInteger sectionCount = [self.tableView numberOfRowsInSection:indexPath.section];
NSInteger newRowIndex = sectionCount - indexPath.row - 1;
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:indexPath.section];

return newIndexPath;
}

然后只需更换

[self.fetchedResultsController objectAtIndexPath:[self objectNewIndexPath:indexPath];
于 2016-05-12T10:24:06.117 回答