Problem chapter 2 core data by tutorials

please in the second chapter i can’t manage to pass “managedcontext” from the app delegate
if i use UIapplication.shared in viewdidload it works but if i try using the code below in AppDelegate i receive a nil managedcontext when trying to run my programm

guard let vc =
window?.rootViewController as? ViewController else {

return true

}

vc.managedContext = persistentContainer.viewContext

best regards

@carlo476 Thanks very much for your question!

The problem is that after setting the managed object context property, you don’t do anything else. What you need to do is put your ViewController inside a NavigationController and then push the new ViewController. In this case, ViewController would be the rootViewController for the UINavigationController.

I hope this helps!

All the best.

but i don’t understand why i need a navigationcontroller, normally i should be able to pass my managedcontext without that just by setting my window and root view controller but i can’t get it to work, is it possible to explain the proper code to share my instance with my view controller ?

thanks a lot in advance

kind regards

@carlo476 Do you still have issues with this?

hey thanks a lot for your message
i’m not able to make the managedcontext connection between the AppDelegate and the viewcontroller, i encountered the same problem in the dog walk chapter
when i use this code:

guard let navController =
window?.rootViewController as? UINavigationController, let viewController =
navController.topViewController as? ViewController else {return true}
viewController.managedContext = coreDataStack.managedContext
my managedcontext in view controller is still nil(of course if use the uiapllication.shared it works just fine)
by the way every time i add the line var window = UIWindow to the AppDelegate(and that bothers me because it exist in the scene delegate but not in the app delegate and that’s i don’t understand very well)
hope someone can help
thanks a lot in advance

i tried my code in the scene delegate and it works
can someone please explain the difference between the AppDelegate and the scene delegate
thanks

Sorry all!

im still seeing “Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value” in " let count = try ! managedContext.count(for: fetch)".
i have tried to either put the code in scene delegate, or embed vc in navigation controller. im a beginner in programming. Could you let me know how to solve this? :frowning:

1 Like

AppDelegate is now split into two files, AppDelegate.swift and SceneDelegate.swift.

The code it says to put in the AppDelegate function didFinishLaunchingWithOptions should go in the SceneDelegate function willConnectTo.

1 Like

it is working now. thanks for your hints and i have figured out the way.
i have decided to delete the scenedelegate and remove the code about it in the AppDelegate

This topic was automatically closed after 166 days. New replies are no longer allowed.