0
votes

i have an app with an On and Off Mode. In Online Mode, i am checking a login to a server,
downloading an XML-File and parsing that File. All Datas are written to coreData.

I am populating my TableView with NSFetchedResultsController.

If i use the Off Mode (To use the Offline mode, the coreData Entitys should not be nil),
i just dismiss the view to display the tableView with the data from coreData.

Problem here is: The data is not sorted correctly if i use the Offline Mode.
reloadData doesn't work, i have tried it more Times.

How can i relaod the tableView, so i get the data correctly sorted in off mode, too??

Edit 1:

In Off mode i only do this:

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

How can i do sort the tableview on viewDidAppear?

Edit 2:
Here my 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;
}    
1
Could you please post some relevant code. I'm having a similar issue, and maybe together we can get it sorted. - ICL1901
What should i post it in your opinion? I dont know what is relevant her. i think, here is no mistake in code, i just dont know what i have to do. may do a new nssort and fetch again on viewDidAppear? - brush51

1 Answers

2
votes

Data stored in the database is not sorted by default. It is your responsibility to sort it. Depending on your data structure you can either add an index to aid in sorting or you can sort by some logical value such as name, lastSeenDate, etc.

Post the code that is retrieving the data from Core Data (probably the creation of your NSFetchedResultsController).

Response to OP 1

You really don't want to sort outside of the NSFetchedResultsController as that involves a lot of effort and code that is really unnecessary. You should be configuring the NSFetchedResultsController to sort in the way that you are going to present it on screen. There really is no reason to do it twice. Sorting is non-destructive and non-persistent.

However if you absolutely must sort twice then you would need to:

  1. Gather the array of objects either via -fetchedObjects or via -sections then -objectAtIndex: and then -objects to obtain the array for a section.
  2. With the array in hand you can call -sortedArrayUsingDescriptors: to produce the final sort.

As I said, you really don't want to do this. You would need to do this at every point that you interacted with the NSFetchedResultsController in your code which in a typical UITableViewController is about 5 points. Lots of extra code for zero value.

An additional question for you: Are you using the NSFetchedResultsController to display the data for the cells? Can you post your -tableView: cellForRowAtIndexPath:?