TableView Crashing/Freezing Because Of Core Data Error

后端 未结 2 706
难免孤独
难免孤独 2021-01-28 18:01

When the user clicks the + button in the navbar, a UIAlert with text prompt comes up. The user then enters a string into the prompt and it should result in a new UITableViewCel

2条回答
  •  一个人的身影
    2021-01-28 18:42

    I'm not seeing where your ManagedObjectContext is declared and hooked into your existing data model. As in, where do you declare your "getter" for accessing it from the persistentStoreCoordinator. Try checking your connection and inserting on viewDidLoad. And check the documentation steps here:

    http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOM.html#//apple_ref/doc/uid/TP40005190-SW1

    Here's an example of it hooking into your perstistantStoreCoordinator

    - (NSManagedObjectContext *) managedObjectContext {
    
    if (managedObjectContext != nil) {
        return managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
    

    }

    Or from the tutorial you're using, you'll see it in the application delegate's didFinishLaunching method :

    NSManagedObjectContext *context = [self managedObjectContext];
    if (!context) {
        // Handle the error.
    }
    // Pass the managed object context to the view controller.
    rootViewController.managedObjectContext = context;
    

    EDIT

    After reviewing your code, you need to do two things :

    1) edit your AppDelegate to load the "Curl" model, not the "Temp" model. That's the name of your xdatamodel.

    2) You need to reference your app delegate's context and NOT make one locally. I.e.

    CurlAppDelegate *curlAppDelegate = [[UIApplication sharedApplication] delegate];
        NSManagedObjectContext *context = [curlAppDelegate managedObjectContext];
    
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:context];
        [request setEntity:entity];
    
        NSError *error = nil;
        NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
        if (mutableFetchResults == nil) {
            // Handle the error.
        }
    

提交回复
热议问题