2012-04-11 54 views
1

我試圖對實體文件夾執行獲取請求,並且我想要一個名爲xyz的文件夾作爲排序後的最後一個對象。NSSortDescriptor不被調用

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Folder" inManagedObjectContext:appDelegate.managedObjectContext]; 

    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *lastDescriptor = 
    [[[NSSortDescriptor alloc] initWithKey:@"folderName" ascending:YES comparator:^NSComparisonResult(NSString* name1, NSString* name2) { 
     NSLog(@"descriptor"); 
     if ([name1 isEqualToString:@"xyz"]) { 
      return NSOrderedAscending; 
     } 
     if ([name2 isEqualToString:@"xyz"]) { 
      return NSOrderedDescending; 
     } 

     return [name1 compare:name2]; 
    }] autorelease]; 


    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:lastDescriptor]]; 
    [fetchRequest setFetchBatchSize:5]; 

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

    [fetchRequest release]; 
    [theFetchedResultsController release]; 

    NSError *error; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 

但是沒有任何一個NSLog語句被調用。他們應該嗎?並且xyz文件夾不會最後出現,但所有內容都按字母順序排序。難道我做錯了什麼?

編輯:發佈完整的代碼

+0

我也許是一個愚蠢的問題,但你是否實際執行取回?這只是建立這種排序。 – danh 2012-04-11 15:40:15

+0

嗯,我確實得到了結果,他們只是沒有正確排序。我最終得到按字母順序排列的結果,儘管這是我打電話時唯一的排序描述符。 – Snowman 2012-04-11 15:42:33

+0

NSLog語句是否應該顯示出來? – Snowman 2012-04-11 15:45:44

回答

1

此代碼(代碼)工作原理:

- (void)sortMe { 

    NSDictionary *d0 = [NSDictionary dictionaryWithObject:@"efg" forKey:@"folderName"]; 
    NSDictionary *d1 = [NSDictionary dictionaryWithObject:@"xyz" forKey:@"folderName"]; 
    NSDictionary *d2 = [NSDictionary dictionaryWithObject:@"abc" forKey:@"folderName"]; 
    NSDictionary *d3 = [NSDictionary dictionaryWithObject:@"def" forKey:@"folderName"]; 
    NSArray *testMe = [NSArray arrayWithObjects:d0, d1, d2, d3, nil]; 

    NSSortDescriptor *lastDescriptor = 
    [[NSSortDescriptor alloc] initWithKey:@"folderName" ascending:YES comparator:^NSComparisonResult(NSString* name1, NSString* name2) { 
     NSLog(@"descriptor"); 
     if ([name1 isEqualToString:@"xyz"]) { 
      return NSOrderedAscending; 
     } 
     if ([name2 isEqualToString:@"xyz"]) { 
      return NSOrderedDescending; 
     } 

     return [name1 compare:name2]; 
    }]; 

    NSArray *sorted = [testMe sortedArrayUsingDescriptors:[NSArray arrayWithObject:lastDescriptor]]; 
    for (NSDictionary *d in sorted) { 
     NSLog(@"value=%@", [d valueForKey:@"folderName"]); 
    } 
} 

我預測這個代碼將工作,太:

NSError *error; 
NSArray *result = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
// log the array 

我覺得問題在代碼中的其他地方位於NSFetchedResultsController委託設置中。

+1

調用這返回一個未排序的數組:NSArray * result = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];但是如果我用相同的排序描述符排序該數組,它就可以工作。這與不應用排序描述符的提取請求有關。我應該從哪裏着手解決這個問題? – Snowman 2012-04-11 16:15:36