Posts

Post not yet marked as solved
5 Replies
719 Views
I'm trying to follow the steps here https://developer.apple.com/documentation/coredata/setting_up_a_core_data_stack?language=objc for setting up core data using objective c, using the boilerplate given by xcode when opting into core data, but it appears to only show swift. The main view controller I have is of class TodoListTableViewController, which is storyboard-generated, which is embedded in a navigation controller like so: entrypoint->navigation controller->TodoListTableViewController I tried defining my AppDelegate::applicationDidFinishLaunchingWithOptions like so: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (self.persistentContainer == nil) { [NSException raise:@"did not initialize persistent container in app delegate" format:@"no init"]; } UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; TodoListTableViewController *todoListVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"TodoListTableViewController"]; todoListVC.persistentContainer = self.persistentContainer; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:todoListVC]; self.window.rootViewController = navController; return YES; } and using the boilerplate in AppDelegate @synthesize persistentContainer = _persistentContainer; - (NSPersistentContainer *)persistentContainer { // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it. @synchronized (self) { if (_persistentContainer == nil) { _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"ToDoList"]; [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) { if (error != nil) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. /* Typical reasons for an error here include: * The parent directory does not exist, cannot be created, or disallows writing. * The persistent store is not accessible, due to permissions or data protection when the device is locked. * The device is out of space. * The store could not be migrated to the current model version. Check the error message to determine what the actual problem was. */ NSLog(@"Unresolved error %@, %@", error, error.userInfo); abort(); } }]; } } return _persistentContainer; } #pragma mark - Core Data Saving support - (void)saveContext { NSManagedObjectContext *context = self.persistentContainer.viewContext; NSError *error = nil; if ([context hasChanges] && ![context save:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, error.userInfo); abort(); } } but I'm getting a missing container exception since I added a check for the existence of persistentContainer in TodoListTableViewController like so: - (void)viewDidLoad { [super viewDidLoad]; if (self.persistentContainer == nil) { [NSException raise:@"missing container" format:@"missing container"]; } } thank you.
Posted
by mbolt.
Last updated
.