CoreData 按 NSDate 排序不起作用?
Posted
技术标签:
【中文标题】CoreData 按 NSDate 排序不起作用?【英文标题】:CoreData Sort By NSDate not work? 【发布时间】:2011-09-13 08:54:32 【问题描述】:在代码中:
- (NSFetchedResultsController *)getFetchedResultsController
if (_fetchedResultsController != nil)
return _fetchedResultsController;
[NSFetchedResultsController deleteCacheWithName:@"LatestNews"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"News" inManagedObjectContext:self.managedObjectContext]];
[request setFetchLimit:kDefaultNewsLimit];
[request setFetchBatchSize:20];
[request setIncludesSubentities:NO];
[request setIncludesPropertyValues:NO];
NSSortDescriptor *dateSortor = [NSSortDescriptor sortDescriptorWithKey:@"dateAdded" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:dateSortor]];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"LatestNews"];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
if (isFirstLoading)
[refreshView refreshLastUpdatedDate];
NSError *error;
if (![self.fetchedResultsController performFetch:&error])
if ([error code])
NSLog(@"%@", [error localizedDescription]);
exit(EXIT_FAILURE);
loadMoreButton.hidden = self.numberOfLimitedNews > 0 ? NO : YES;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
[self.tableView setContentOffset:CGPointMake(0, -70) animated:YES];
[refreshView setState:EGOOPullRefreshLoading];
isRefreshing = YES;;
[self performSelector:@selector(fetchNewsFromInternet) withObject:nil afterDelay:2];
isFirstLoading = NO;
结果:
2011-09-13 16:48:20.959 HJNews[5971:bc03] 2011-02-04 06:00:00 +0000
2011-09-13 16:48:21.079 HJNews[5971:bc03] 2011-09-13 08:29:48 +0000
2011-09-13 16:48:21.230 HJNews[5971:bc03] 2011-02-19 01:30:00 +0000
2011-09-13 16:48:21.463 HJNews[5971:bc03] 2011-09-13 08:45:00 +0000
2011-09-13 16:48:21.879 HJNews[5971:bc03] 2011-03-06 02:00:00 +0000
2011-09-13 16:48:22.143 HJNews[5971:bc03] 2010-12-01 02:40:00 +0000
2011-09-13 16:48:22.229 HJNews[5971:bc03] 2011-09-13 02:03:43 +0000
2011-09-13 16:48:22.313 HJNews[5971:bc03] 2011-09-13 08:10:33 +0000
2011-09-13 16:48:22.446 HJNews[5971:bc03] 2011-01-02 06:00:00 +0000
2011-09-13 16:48:22.627 HJNews[5971:bc03] 2011-09-13 08:45:00 +0000
2011-09-13 16:48:22.978 HJNews[5971:bc03] 2011-01-23 02:00:01 +0000
2011-09-13 16:48:23.092 HJNews[5971:bc03] 2011-09-13 03:35:40 +0000
2011-09-13 16:48:23.196 HJNews[5971:bc03] 2011-02-18 06:20:00 +0000
2011-09-13 16:48:23.346 HJNews[5971:bc03] 2011-09-13 00:00:00 +0000
2011-09-13 16:48:23.812 HJNews[5971:bc03] 2011-02-08 06:00:00 +0000
2011-09-13 16:48:23.980 HJNews[5971:bc03] 2011-09-12 01:40:10 +0000
2011-09-13 16:48:24.179 HJNews[5971:bc03] 2011-01-18 06:00:00 +0000
2011-09-13 16:48:26.257 HJNews[5971:bc03] 2010-12-22 06:30:00 +0000
2011-09-13 16:48:27.783 HJNews[5971:bc03] 2011-02-08 06:00:00 +0000
2011-09-13 16:48:27.784 HJNews[5971:bc03] 2011-09-12 01:40:10 +0000
2011-09-13 16:48:27.785 HJNews[5971:bc03] 2011-01-18 06:00:00 +0000
2011-09-13 16:48:27.785 HJNews[5971:bc03] 2010-12-22 06:30:00 +0000
2011-09-13 16:48:27.786 HJNews[5971:bc03] 2011-09-13 02:10:17 +0000
我不知道为什么它不起作用,我似乎是随机排序的……有人帮我吗?
第一次加载正常,点击加载更多按钮或更改(category navigation bar)时异常
【问题讨论】:
什么是添加日期?是 NSDate 吗? 是的。是服务器数据库时间添加的消息。 嗯。您能否向我们展示获取获取结果并写出上述诊断信息的代码? 尝试删除缓存。将缓存名称设置为 nil。 我删除了缓存,但问题没有解决... 【参考方案1】:再次检查您的数据模型中dateAdded
的属性类型是否设置为Date
,在您的类文件中设置为NSDate
。
【讨论】:
是的,我查过了,它们是 NSDate 类型;-( 检查您的输出不是来自NSSet
(未排序),而是来自NSArray
(已排序)。
我查找了 NSFetchedResultsController 的 fetchedObjects 类型,它是 _PFMutableProxyArray,但是当我更改谓词时(单击导航类别的顶部,请参阅image),它更改为 _PFArray
第一次加载正常,点击加载更多按钮或更改(分类导航栏)时异常以上是关于CoreData 按 NSDate 排序不起作用?的主要内容,如果未能解决你的问题,请参考以下文章