How to load Core Data to a view other than RootViewController?

Mostly I have basic data, and the application works correctly, with the exception of the code in AppDelegate. The code I'm having problems with is the following:

- (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];

}

I do not want manageObjectContext to run the root controller. I want to make this another controller. However, if I change the classes to a view controller for which I need it, it loads that view controller when the application starts, which I don’t want to do. I still want to run root mode, but I want to be able to load the main data context for my other view controller. I am really confused about how to fix this problem. I spent 2 days finding a way to fix this, but so far no luck. Any help would be appreciated.

In addition, if the following is not specified in the appdelegate application:

RootViewController *tableController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
        tableController.managedObjectContext = [self managedObjectContext];

        self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
        [tableController release];

I get this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Hello'

EDIT: Here is the object code:

- (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];

}

Also, if I use my own appdelegate called MyAppDelegate

MyAppDelegate *tableController = [[MyAppDelegate alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];

I get the following error:

Object cannot be set- either readonly property or no setter found
+3
3

, ? managedObjectContext tableController .

- viewController "" ObjectContext . , CoreData, AppDelegate, , . managedObjectContext , managedObjectContext .

AppDelegate *theDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = theDelegate.managedObjectContext;

PS1: , AppDelegate .

PS2: , , , CoreData .

0

RootViewController , . , View . , RootViewController View .

, , . , , .

0

+entityForName:, +entityForName:.

, , :

entity = [NSEntityDescription entityForName:@"Hello" 
                     inManagedObjectContext:managedObjectContext];
                                            ^^^^^^^^^^^^^^^^^^^^

, managedObjectContext getter. , .
, ObjectContext . , .

:

entity = [NSEntityDescription entityForName:@"Hello" 
                     inManagedObjectContext:self.managedObjectContext];
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^

, - :

tableController.managedObjectContext = [self managedObjectContext];
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^

, . .

[self managedObjectContext] self.managedObjectContext

- , + entityForName: inManagedObjectContext:

0

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


All Articles