2011-09-13 63 views
1

代碼:CoreData排序方式NSDate不工作?

- (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

+0

dateadded是什麼?它是一個NSDate嗎? –

+0

是的。這是在服務器數據庫時間添加的新聞。 –

+0

嗯。你能告訴我們代碼,取得結果並寫出上面的診斷嗎? –

回答

0

仔細檢查你有dateAdded設置你的屬性類型,在數據模型中DateNSDate在類文件。

+0

是的,我檢查了它們是NSDate類型;-( –

+0

檢查你的輸出是不是來自'NSSet'(它沒有排序),而是'NSArray'(這是)。 – Mundi

+0

我查找NSFetchedResultsController的fetchedObjects類型,它是_PFMutableProxyArray,但是當我更改謂詞時(單擊導航類別的頂部,請參閱[image](http://www.firestudio.cn/wp-content/uploads/2011 /09/2011-09-15-10.39.57.png))它更改爲_PFArray –