CoreData UITableViewController managedObjectContext Error

I started playing with CoreData and taking a new project with CoreData and creating these funds in my own project. I reached the stage where I more or less identical duplicated a new project, however I get an error message.

This line, controller.managedObjectContext = self.managedObjectContext; causes me problems. When I comment on this, the application just starts with an empty UITableView, however, if I include a line, this causes this error:

 Universal[24718:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewController setManagedObjectContext:]: unrecognized selector sent to instance 0x1521a0' *** First throw call stack: (0x344558bf 0x346a51e5 0x34458acb 0x34457945 0x343b2680 0x2413 0x378367eb 0x378303bd 0x377fe921 0x377fe3bf 0x377fdd2d 0x30c30df3 0x34429553 0x344294f5 0x34428343 0x343ab4dd 0x343ab3a5 0x3782f457 0x3782c743 0x2331 0x22c8) terminate called throwing an exception 

I need more code to look, I can provide it, and I hope you have an idea what is going on. I do not see anything that could cause this, I do not get any errors, but I get this log, because this line causes the whole application to crash.

The string is in the didFinishLaunchingWithOptions method of the didFinishLaunchingWithOptions delegate, as in the new kernel data project.

As requested, the header file for the table view controller:

 #import <UIKit/UIKit.h> #import <CoreData/CoreData.h> @interface myTableViewController : UITableViewController <NSFetchedResultsControllerDelegate> @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController; @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; @end 
+4
source share
4 answers

The reason you get the error is in your AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; MasterViewController *controller = (MasterViewController *)navigationController.topViewController; controller.managedObjectContext = self.managedObjectContext; return YES; } 

When using the CoreData template, the default context for the managed objects is assigned to topViewController, which is no longer the MasterViewController, because you inserted another view as a starting point. Thus, an unrecognized selector is sent to the instance.

You have two options:

1) Delete the code that assigns the context of the managed object, and in the view it needs, get it like this:

  [[[UIApplication sharedApplication] delegate] mainManagedObjectContext]; 

2) Save the code (correct the class name), add an declaration for managedObjectContext and pass the NSManagedObjectContext object throughout your application between all your UIViewControllers

+6
source

You are missing the wiring that binds the UIViewController (or subclasses) to the NSFetchedResultsController . You get it for free when you set up a new project with Core Data enabled, but if you add Core Data to an existing project, you need to link a few things yourself.

For more information see http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/

+2
source

It depends on how your application is structured. If you use TabBarViewController in front of all the code, it is a little different

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UITabBarController *tabController = (UITabBarController *)self.window.rootViewController; UINavigationController *navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:0]; MasterViewController *controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0]; controller.managedObjectContext = self.managedObjectContext; return YES; } 
+2
source

I had the same problem. I solved this by making sure that the ManagedContextObject property is included in the root view controller (the very first view controller connected to the very first navigation controller), regardless of whether it uses it.

in .h of the Root View controller:

 @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; 

in .m

 @synthesize managedObjectContext; 

Apologizes if my answer is a little dumbfounded. In order to understand them, I must be unthinkable for myself :)

+1
source

Source: https://habr.com/ru/post/1385358/


All Articles