2

I have quite an ordinary RSS-reader type App. The objects in the Core Data have a pubDate attribute which is the exact date + time of publishing. Using this for sections gives every article its own section.

Therefore I added a transient attribute:

@dynamic sectionDay;

-(NSString *) sectionDay {  
    // Get the date, and convert it to the day (counld probably be done a bit more scientific!)
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

    //Set the required date format
    [formatter setDateStyle:NSDateFormatterLongStyle];

    // Set the values;
    NSString *dateString = [formatter stringFromDate:self.pubDate]; 

    return dateString;
}

Then in the FetchedResultsController I added sectionDay as the sectionNameKeyPath

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"pubDay" ascending:NO] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:@"sectionDay" cacheName:nil];

The problem is that I get an "ojbc_exception_throw" on NSFetchedResultsController PerformFetch:

*

#0  0x012165a8 in objc_exception_throw
#1  0x00eba676 in -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:]
#2  0x00df2a78 in -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:]
#3  0x00df2881 in -[NSSQLAdapter newSelectStatementWithFetchRequest:]
#4  0x00df272e in -[NSSQLCore newRowsForFetchPlan:]
#5  0x00df1ab5 in -[NSSQLCore objectsForFetchRequest:inContext:]
#6  0x00df166e in -[NSSQLCore executeRequest:withContext:error:]
#7  0x00ea10ec in -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
#8  0x00dee807 in -[NSManagedObjectContext executeFetchRequest:error:]
#9  0x00ed2c10 in -[NSFetchedResultsController performFetch:]
#10 0x0000456e in -[ArticleController getArticles] at ArticleController.m:92

*

Any suggestions what I do wrong?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Luuk D. Jansen
  • 4,402
  • 8
  • 47
  • 90

1 Answers1

2

Edit: Solution in first comment by BoltClock!

As transient properties are not stored in the database, there is no way for the fetched results controller to query for them with sql statements.(your error message)

A possible solution would be to additionally store a normalized date (remove the time component, set to 00:00:00) to group entries by date without time.

Apple recommends this type of solution in some cases, like storing normalized strings for searching

Martin Brugger
  • 3,168
  • 26
  • 20
  • 2
    Transient properties can still be used here, the date strings used for table sections just need to be computed based on the actual `pubDate` attribute, instead of being queried from the persistent store. Of course, this answer is an equally alright way of going about it. – BoltClock Dec 30 '10 at 08:41
  • You are right, just had a look at the documentation of NSFetchedResultsController. Just for grouping sections, my solution seems to be a little overkill – Martin Brugger Dec 30 '10 at 10:06
  • I got the App working with the transient properties, as was suggested i other posts. Thanks for the confirmation that it should work! I must have made a type in my first attempt and it works now! – Luuk D. Jansen Dec 30 '10 at 11:05
  • Nah I don't think it's overkill. It's fine! – BoltClock Dec 30 '10 at 11:47