3
和它調用viewDidLoad
我執行取請求和退休陣列或我的核心數據對象:CoreData performFetch在viewDidLoad中當我的主視圖控制器被加載不工作
+ (NSArray *)getData {
// Fetch Data
NSError *error = nil;
if (![[[AppDelegate instance] fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSArray *items = [[AppDelegate instance].fetchedResultsController fetchedObjects];
return items;
}//end
出於某種原因,這將返回一個當從viewDidLoad
調用空數組。
如果我把從viewDidAppear:
它正常工作,並返回我的CoreData
對象的NSArray
同樣的方法。
有沒有理由不能在viewDidLoad
?
編輯:
讀取的結果控制方法:
/**
* The controller the gets our results from core data
*
* @version $Revision: 0.1
*/
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create and configure a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SiteConfig" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array
NSSortDescriptor *sectionTitle = [[NSSortDescriptor alloc] initWithKey:@"createdDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sectionTitle, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
// Memory management
return fetchedResultsController;
}//end
我寧願讓我的應用程序委託準備好它。我應該在'application:didFinishLaunchingWithOptions:'中做些什麼來準備'fetchedResultsController'? –
我相信,你可以確認,'viewDidLoad'視圖控制器後面調用'application:didFinishLaunchingWithOptions:'。你現在準備好fetchedResultsController了嗎? –
我用'fetchedResultsController'代碼更新了我的問題。 –