2013-07-31 32 views
1

我在我的核心數據存儲(100條)的消息列表最新。CoreData升:是的,顯示從底部

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:YES]; 
NSArray *sortDescriptors = @[sortDescriptor]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 
[fetchRequest setFetchLimit:20]; 

它只顯示我需要的順序中的前20條消息。 我需要做的是:顯示最後20條消息,按創建日期排序,但它們應該出現在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; 
} 
+0

你有'(所有者=%@)'的時候,你應該有'(所有者==%@)' – Abizern

+0

是不一樣的? –

+0

一個是賦值運算符,另一個是等於運算符。 – Abizern

回答

0

所有你應該需要做的是改變sortDescriptor使用ascending: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 objectAtIndexPath:indexPath]; 
+0

你能告訴我一個例子嗎?我如何可以在fetchedResultsController中返回?我附上你的代碼。 –

+0

我無法使用數組。因爲我稱之爲:[self.fetchedResultsController objectAtIndexPath:indexPath] –

0

@Clever答案爲我工作更換

[self.fetchedResultsController objectAtIndexPath:indexPath]; 

。我只撥打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];