【问题标题】:Tableview with sections per day (with Core Data)每天有部分的表格视图(使用核心数据)
【发布时间】:2011-06-01 01:41:36
【问题描述】:

我有一个很普通的 RSS 阅读器类型的应用程序。 Core Data 中的对象有一个 pubDate 属性,它是发布的确切日期+时间。将其用于部分可为每篇文章提供自己的部分。

因此我添加了一个瞬态属性:

@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 PerformFetch 上得到一个“ojbc_exception_throw”:

*

#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

*

任何建议我做错了什么?

【问题讨论】:

标签: iphone uitableview core-data


【解决方案1】:

编辑:BoltClock 第一条评论中的解决方案!

由于瞬态属性不存储在数据库中,因此获取结果控制器无法使用 sql 语句查询它们。(您的错误消息)

一个可能的解决方案是额外存储一个标准化日期(删除时间组件,设置为 00:00:00),以便按日期对条目进行分组,而不是时间。

Apple 在某些情况下推荐这种类型的解决方案,例如存储标准化字符串以进行搜索

【讨论】:

  • 这里仍然可以使用瞬态属性,用于表部分的日期字符串只需要根据实际的pubDate 属性计算,而不是从持久存储中查询。当然,这个答案也是一种同样好的解决方法。
  • 你说得对,刚刚看了 NSFetchedResultsController 的文档。只是为了分组部分,我的解决方案似乎有点矫枉过正
  • 我让应用程序使用瞬态属性,正如其他帖子所建议的那样。感谢您确认它应该可以工作!我一定是在第一次尝试时做了一个类型,现在它可以工作了!
  • 不,我不认为这是矫枉过正。没关系!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
相关资源
最近更新 更多