0

我有一个具有开启和关闭模式的应用程序。在在线模式下,我正在检查服务器的登录、
下载 XML 文件并解析该文件。所有数据都写入 coreData。

我正在使用 NSFetchedResultsController 填充我的 TableView。

如果我使用关闭模式(要使用离线模式,coreData 实体不应该为零),
我只是关闭视图以显示带有 coreData 数据的 tableView。

这里的问题是:如果我使用离线模式,数据排序不正确。
reloadData 不起作用,我尝试了更多次。

我怎样才能重新加载 tableView,所以我也可以在关闭模式下正确排序数据?

编辑1:

在关闭模式下,我只这样做:

if (xmlFile) {  
    //FadeOut animation nach erfolgreichem Login
    //und removeFromSuperview
    [UIView animateWithDuration:0.8 
        animations:^{loginView.alpha = 0.0;}
        completion:^(BOOL finished){ [loginView removeFromSuperview]; }];
}  

如何对 viewDidAppear 上的 tableview 进行排序?

编辑 2:
这里是我的 NSFetchedResultsController:

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil)
{
    return __fetchedResultsController;
}

/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];

// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntitySetsCards" inManagedObjectContext:self.managedObjectContext];

NSPredicate *inboxPred = [NSPredicate predicateWithFormat:@"archived == 0"];


[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
[fetchRequest setPredicate:inboxPred];


// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptorSetOrder = [[[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES] autorelease];
NSSortDescriptor *sortDescriptorColorOrder = [[[NSSortDescriptor alloc] initWithKey:@"colorOrder" ascending:YES] autorelease];
NSSortDescriptor *sortDescriptorSortOrder = [[[NSSortDescriptor alloc] initWithKey:@"sortingOrder" ascending:YES] autorelease];

NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptorSetOrder, sortDescriptorSortOrder, sortDescriptorColorOrder, nil] autorelease];

[fetchRequest setSortDescriptors:sortDescriptors];


// nil for section name key path means "no sections".
// cacheName auf nil gesetzt, da @"Root" fehler erzeugt hat (FATAL ERROR)
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"setTitle" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;


NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

allFetchedCards = aFetchedResultsController;
allCards = [__fetchedResultsController fetchedObjects];

return __fetchedResultsController;
}    
4

1 回答 1

2

存储在数据库中的数据默认不排序。您有责任对其进行分类。根据您的数据结构,您可以添加索引以帮助排序,也可以按一些逻辑值(例如名称、lastSeenDate 等)进行排序。

发布从 Core Data 中检索数据的代码(可能是您的NSFetchedResultsController.

对 OP 1 的回应

你真的不想在之外进行排序,NSFetchedResultsController因为这涉及大量的工作和真正不必要的代码。您应该将其配置NSFetchedResultsController为按照您将在屏幕上显示的方式进行排序。真的没有理由做两次。排序是非破坏性和非持久性的。

但是,如果您绝对必须排序两次,那么您需要:

  1. -fetchedObjects通过 then或 via -sectionsthen收集对象数组,-objectAtIndex:然后-objects获取一个部分的数组。
  2. 使用手中的数组,您可以调用-sortedArrayUsingDescriptors:以生成最终排序。

正如我所说,你真的不想这样做。您需要在与NSFetchedResultsController代码交互的每一点都执行此操作,通常UITableViewController约为 5 点。零值的许多额外代码。

对您的另一个问题:您是否使用NSFetchedResultsController来显示单元格的数据?可以发你的-tableView: cellForRowAtIndexPath:吗?

于 2012-01-16T17:44:22.917 回答