2013-12-08 36 views
0

我在沒有NSFetchedResultsController的情況下設置了CoreData,並且一切都很好。切換到NSFetchedResultsController後,嘗試保存圖像時出現奇怪的錯誤。將NSFetchedResultsController添加到項目後出現的問題

這裏是我用來保存圖片代碼:

- (void)saveImage { 

    NSManagedObjectContext *context = [self managedObjectContext]; 

    TimeTravelFeed *timeTravelFeed = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTravelFeed" inManagedObjectContext:context]; 

    NSData *imageData = UIImageJPEGRepresentation(self.thumbImage, 0.8f); 

    [timeTravelFeed setValue:imageData forKey:@"imageData"]; 

    NSError *error = nil; 

    if (![self.managedObjectContext save:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    //[tableView reloadData];   
} 

,這裏是錯誤消息:

-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x1669fb40 
2013-12-08 10:09:49.442 Time Travel[830:60b] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. 
-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x1669fb40 with userInfo (null) 
2013-12-08 10:09:49.443 Time Travel[830:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_PFExternalReferenceData compare:]: unrecognized selector sent to instance 0x1669fb40' 

這裏是NSFetchedResultsController代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailViewController *detailsViewController = [[DetailViewController alloc] init]; 
    [self.navigationController pushViewController:detailsViewController animated:YES]; 

} 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 
} 

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"TimeTravelFeed" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
           initWithKey:@"imageData" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:managedObjectContext sectionNameKeyPath:nil 
                cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 

} 
+1

我不能看到你的這部分代碼錯誤。也許在「所有Objective-C異常」中設置一個斷點來定位問題。 –

+0

我應該在哪裏調用它?添加NSFEtchedResultsController的代碼後,我不知道需要添加到用於保存圖像的舊代碼塊中?我需要添加什麼? @nhgrif – matthew

+0

NSFetchedResultsController用於在表視圖中顯示Core Data對象,並且完全獨立於* add *對象的方式。正如我所說,你的代碼似乎沒問題,而問題在別的地方。 - 您必須以某種方式查找問題(例如,使用異常斷點)。 –

回答

2

這個問題似乎是在這裏:

NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
          initWithKey:@"imageData" ascending:NO]; 

無法排序在「二進制數據」屬性。取得的結果控制器 需要一個排序描述符,所以你應該使用不同的屬性,例如一個字符串,數字或日期。例如

NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
          initWithKey:@"creationDate" ascending:NO]; 

其中「creationDate」是一個屬性(類型「日期」),並創建對象時設置:

[timeTravelFeed setValue:imageData forKey:@"imageData"]; 
[timeTravelFeed setValue:[NSDate date] forKey:@"creationDate"]; 
+0

你可以排序最新到最舊的。我只在實體中有一個屬性,我試圖在表 – matthew

+0

中顯示,我同意Martin的觀點。放置一個像日期這樣的附加屬性並用它來排序對象。 @ user3072540 –

+0

@ user3072540:然後,您必須添加「creationDate」屬性(類型「Date」)並在排序描述符中使用該屬性。創建對象時,設置'[timeTravelFeed setValue:[NSDate date] forKey:@「creationDate」];' –

1

轉到您的DBfile,選擇名爲「TimeTravelFeed」的實體並選擇鍵「imageData」。設置屬性。

+0

這已經過檢查。 – matthew

+0

NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@「imageData」ascending:NO];我認爲排序描述符無法與此相比較。 – Slashik

+0

嘗試添加任何其他屬性。也許文件大小或時間戳進行排序。 – Slashik

相關問題