Steaphann
Steaphann

Reputation: 2777

sorting a fetchrequest on date

I have a core data database that is filled up with data coming from a json webservice. But I have a problem with the date. This date is coming back like a string. So first thing I want to do is to transform this string to a NSDate. The second thing is to transform this NSDate in the right format so that I can add a sort descriptor on date.

This is what I have at the moment for how I store my date in the core data database.

 NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MM/dd/yyyy"];
    NSDate* d = [df dateFromString:[genkInfo objectForKey:CALENDAR_DATE]];

    kalender.date              = d;

And how I get my data.

- (void)getKalender // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Kalender"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.genkDatabase.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];

}

Any idea how to do this?

Kind regards and thank in advance

EDIT

When I do the following log. I get for each record the correct date and a null value

  NSLog(@"date  = %@",kalender.date);

    2012-10-08 19:17:12.125 RacingGenk[596:c07] date  = 2013-09-02 22:00:00 +0000
    2012-10-08 19:17:12.125 RacingGenk[596:c07] date  = (null)

Upvotes: 1

Views: 300

Answers (1)

Martin R
Martin R

Reputation: 540065

As it turned out in the discussion, the format used by the date formatter did not match for date format sent by the web service. Using

[dateFormatter setDateFormat:@"dd/MM/yyyy"]

fixed the problem.

Upvotes: 1

Related Questions