我試圖對實體文件夾執行獲取請求,並且我想要一個名爲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文件夾不會最後出現,但所有內容都按字母順序排序。難道我做錯了什麼?
編輯:發佈完整的代碼
我也許是一個愚蠢的問題,但你是否實際執行取回?這只是建立這種排序。 – danh 2012-04-11 15:40:15
嗯,我確實得到了結果,他們只是沒有正確排序。我最終得到按字母順序排列的結果,儘管這是我打電話時唯一的排序描述符。 – Snowman 2012-04-11 15:42:33
NSLog語句是否應該顯示出來? – Snowman 2012-04-11 15:45:44