Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13850

How to ignore sortDescriptor in NSFetchedResultsController

I'm using NSFetchedResultsController to retrieve data for a UITableView. but problem is I have to use sortDescriptor with NSFetchedResultsController. I don't want to sort my data, I want to show data in the table as the order I have inserted them in the table.

Is there any way to handle this task?

Upvotes: 1

Views: 1400

Answers (2)

Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13850

thanks for your answer. I have added another property named sorting number and implement this way:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
chapterSN = [userDefaults objectForKey:@"sortingNumber"];
if (chapterSN == Nil) {
    NSInteger chapterSortingValue = 0;
    chapterSN = [NSNumber numberWithInteger:chapterSortingValue];
    [userDefaults setValue:chapterSN forKey:@"sortingNumber"];
 }
 else {
     NSInteger chapterSortingValue = [chapterSN integerValue];
     chapterSortingValue = chapterSortingValue+1;
     chapterSN = [NSNumber numberWithInteger:chapterSortingValue];
     [userDefaults setValue:chapterSN forKey:@"sortingNumber"];
 }

Upvotes: 1

trapper
trapper

Reputation: 11993

If you want your data sorted by the order they are inserted then you need a field to record this order.

A nice way to do this is to add a creationDate property to your object, then use the awakeFromInsert method to automatically record the insertion time.

- (void)awakeFromInsert
{
    [super awakeFromInsert];
    [self setValue:[NSDate date] forKey:@"creationDate"];
}

Now when you want to sort them by insertion order you are all set.

Upvotes: 6

Related Questions