lostAtSeaJoshua
lostAtSeaJoshua

Reputation: 1755

How to fetch Core Data by the order the data was added and display in that order in a UITableView?

I am displaying an entity called Skills in a UITableViewController. I fetch the results like this in the viewDidLoad:

-(void)fetchTableData {
    NSManagedObjectContext *context = [self managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Skills" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSError *error;

    self.skillsArray = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
}

Also my cell for index path is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...

    Skills *skill = self.skillsArray[indexPath.row];

    // Skills is a NSManagedObject, I added the Skills.h file.

    [cell.textLabel setText:skill.nameOfSkill];

    return cell;
}

And I am adding new NSManagedObject *newSkill to Core Data by using UIAlertView with a text field in the delegate method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
      NSLog(@"Add button clicked");
      NSString *newSkillText = [alertView textFieldAtIndex:0].text;
      NSManagedObjectContext *context = [self managedObjectContext];
      Skills *newSkill = [NSEntityDescription insertNewObjectForEntityForName:@"Skills" inManagedObjectContext:context];
      newSkill.nameOfSkill = newSkillText;
      [self.skillsArray addObject:newSkill];
    } else {
       // Do something else
    }
    [self.tableView reloadData];
}

Every time I reload the data the cells are displaying the data in the order the data was added but if dismiss the view controller and return the cells display the data in a different order than added? The weird part is that I am using this same exact code to add core data and retrieve it in another UITableViewController and it never displays out of order. The data added in this UITableViewController is as follows: I am pushing to another UIViewController and add the information there and then dismiss back to the tableview. In this code I am adding the information while in the view controller is being presented, maybe that could have something to do with it?
Also I know I could add an NSSortDiscriptor such as:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"nameOfSkill" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];

but it does it by the name and I want by the way it was added without having to add another attribute for index cause I never did that to my other data and it always displays in the order it was added.

Upvotes: 2

Views: 1133

Answers (3)

Chetan
Chetan

Reputation: 2124

You won't get the data as it is. You will have to insert a field of "Time" and you can sort according to it.

OR

Add a unique field of 'data_id' . Always check the count before inserting the data. And give the data_id accordingly adding 1 to the count. Then after fetching the data from core data sort it as per data_id.

You can do as per you like.

Upvotes: 1

shubhangi
shubhangi

Reputation: 19

For the desired result you need to sort it according to timestamp or primary key which is auto-generated by core data database.

Core Data makes its own primary key - you don't have to add one. You can retrieve it with

NSManagedObjectID *moID = [managedObject objectID];

Upvotes: 0

Hal Mueller
Hal Mueller

Reputation: 7665

You'll have to add an Attribute to sort on...either an updating, incrementing counter, or the timestamp of the insertion. If you subclass NSManagedObject, ou can write this value in -awakeFromInsert. Then your tableview's fetch request will sort on that attribute.

Upvotes: 2

Related Questions