2014-09-10 58 views
0

我有一個fetchrequest。現在我需要從數據庫中「追蹤」點,但是當沒有追蹤點時,他們都需要檢索。當1ste失敗時,NSFetchController與其他謂詞一起獲取

我該怎麼做?我現在在我的fetchcontroller上設置了一個謂詞,但是當它沒有找到任何跟蹤點時,它只是不顯示任何東西。那麼我可以在哪裏掛上這個,然後說取回所有東西...

有沒有辦法聽到沒有發現什麼或如何做到這一點?

編輯:

- (NSFetchedResultsController *)fetchedResultsController { 

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rank" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]]; 
    [fetchRequest setEntity:entity]; 

/* NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"Auto"]; 
    [fetchRequest setPredicate:predicate];*/ 

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

    [fetchRequest setFetchBatchSize:20]; 

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

    return _fetchedResultsController; 

} 
+0

我建議你做的方法:'fetchWithPredicateOrAllIfNoCountsNone:',與謂詞先取出,如果取數據的「count」爲「0」,取「全部」。 – Larme 2014-09-10 13:52:49

+0

是的,但什麼時候?這是我第一次使用它。那麼我可以在哪裏執行抓取。因爲它現在如何發生是神奇的。 – user1007522 2014-09-10 13:54:29

+0

我用我的代碼編輯了我的帖子。 – user1007522 2014-09-10 13:55:17

回答

0

由於Larme這是解決方案:

- (NSFetchedResultsController *)fetchedResultsController { 

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rank" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]]; 
    [fetchRequest setEntity:entity]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"DON"]; 
    [fetchRequest setPredicate:predicate]; 

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

    [fetchRequest setFetchBatchSize:20]; 

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

    if ([[_fetchedResultsController fetchedObjects] count] == 0) 
    { 
     NSLog(@"0 found"); 
     [fetchRequest setPredicate:nil]; 
    } 

    return _fetchedResultsController; 

}