我基本上有核心數據和應用程序正常工作,除了AppDelegate中的代碼。我有問題的代碼如下:如何在除RootViewController之外的另一個視圖上加載核心數據?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
RootViewController *tableController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
[tableController release];
[window addSubview: [self.navigationController view]];
[window makeKeyAndVisible];
}
我不想讓managedObjectContext經推出根視圖控制器。我想讓它成爲另一個視圖控制器。但是,如果我將類更改爲需要它的視圖控制器,它會在啓動應用程序時加載該視圖控制器,這不是我想要的。我仍然想啓動根視圖,但我想能夠加載我的其他視圖控制器的核心數據上下文。我很困惑如何解決這個問題。到目前爲止,我花了2天的時間試圖找到解決方法,但沒有運氣。任何幫助,將不勝感激。
另外,如果我離開了在的appdelegate didfinishlaunching如下:
RootViewController *tableController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
[tableController release];
我得到這個錯誤:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Hello'
編輯: 這裏是實體代碼:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Lap Times";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTime:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
[self fetchRecords];
}
- (void)addTime:(id)sender {
addTimeEvent *event = (addTimeEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"addTime" inManagedObjectContext:self.managedObjectContext];
[event setTimeStamp: [NSDate date]];
NSError *error;
if (![managedObjectContext save:&error]) {
// This is a serious error saying the record could not be saved.
// Advise the user to restart the application
}
[eventArray insertObject:event atIndex:0];
[self.tableView reloadData];
}
- (void)fetchRecords {
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"addTime" inManagedObjectContext:self.managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Define how we will sort the records
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
// Handle the error.
// This is a serious error and should advise the user to restart the application
}
// Save our fetched data to an array
[self setEventArray: mutableFetchResults];
[mutableFetchResults release];
[request release];
}
另外,如果我使用我自己的appdelegate叫做MyAppDelegate
MyAppDelegate *tableController = [[MyAppDelegate alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
我得到以下錯誤:
Object cannot be set- either readonly property or no setter found
其實他傳遞managedObjectContext的方法比要求更好上下文的AppDelegate。但是,只有當應用程序需要重構才能使用不同的上下文進行插入時,人們纔會意識到這一點。或者,如果控制器將用於不同的應用程序。 – 2011-02-13 10:03:48
我完全同意!讓控制器詢問上下文會在兩者之間產生不必要的耦合。 – Rog 2011-02-13 10:11:21