kavitha
kavitha

Reputation: 1

Avoid Sorting Using NSSortDescriptor

Iam using sqlite database details to display in a tableview.Using NSSortDescriptor Iam able to display the table row contents in ascending or descending order.Please help me to avoid the sorting process and display contents in tableview as they are entered. Here is the code that I was using.It works pretty well by sorting the data,help me to avoid sorting. -

 (NSFetchedResultsController *)fetchedResultsController {
          // Set up the fetched results controller if needed.
 if (fetchedResultsController == nil) {
    // Create the fetch request for the entity.
 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"book"   inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];     
    // Edit the sort key as appropriate.
  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]            initWithKey:@"name" ascending:YES];
  NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
  NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
  NSLog(@"inside table view %@",aFetchedResultsController);     
    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];
    }
  return fetchedResultsController;
    }    

Upvotes: 0

Views: 622

Answers (1)

thewormsterror
thewormsterror

Reputation: 1618

I am assuming you use core data with sql lite. If so you should use a NSFetchedResultsController, there are many examples of how to use that on the web.

NSFetchedResultsController requires a sort descriptor, if you want your values out of order remember that you are populating your table view based on an indexPath reference to an element in your controller's fetched results (-objectAtIndexPath:).

So you can have that give back random indexpaths with rows inferior to row count

Upvotes: 1

Related Questions