我有一個問題,最初沒有加載表視圖,但它工作n另一個.m文件我有。這裏是工作的一個:
添加到您的viewDidLoad:
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
,這對實現塊:
#pragma mark -
#pragma mark Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"OMFrackinG" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
//分組和排序可選
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"state" ascending:YES];// was name
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
propriate.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"state" cacheName:nil];//@"state"
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];
}
return fetchedResultsController;
}
請注意,在另一個線程中獲取數據之後,UI的任何更改都必須在主線程中完成。 – 2011-05-25 22:43:13
這聽起來很可怕。我以前沒有處理過多個線程。我可以使用Grand Central Dispatch嗎?你有任何可以工作的例子嗎? – 2011-05-25 22:44:00
有幾個有用的指南,但我建議[這](http://cocoasamurai.blogspot。COM/2009/09 /導向到塊-隆重-中央dispatch.html)。我已經編輯了一個關於你最終會做什麼的總體思路。 – 2011-05-25 22:54:45