我有一个普通的RSS阅读器类型的应用程序。 Core Data中的对象具有pubDate属性,该属性是发布的确切日期+时间。使用this for sections为每篇文章提供了自己的部分。
因此我添加了一个瞬态属性:
@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;
}
然后在FetchedResultsController中我添加了sectionDay作为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];
问题是我在NSFetchedResultsController上获得了“ojbc_exception_throw”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
*
有什么建议我做错了吗?
答案 0 :(得分:2)
编辑:BoltClock的第一条评论中的解决方案!
由于瞬态属性未存储在数据库中,因此获取的结果控制器无法使用sql语句查询它们。(您的错误消息)
一种可能的解决方案是额外存储一个标准化日期(删除时间组件,设置为00:00:00),按日期对条目进行分组,无需时间。
Apple在某些情况下建议使用此类型的解决方案,例如存储用于搜索的规范化字符串