开发者

Using core data in an existing project

I need to use core data to persist data for my p开发者_如何转开发roject, what I have done so far compiles well, but when I actually start storing things using core data, the program just quits, and I don't know the reason. I set up all the required components for core data in the appDelegate file, and I want to store data in a viewController called DetailViewController. Here is what I have done:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Set the tab bar controller as the window's root view controller and display.
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

//this is what I added, reference managedObjectContext in the detail view controller.

    detailViewController = [[DetailViewController alloc] init];
    detailViewController.managedObjectContext = [self managedObjectContext];

    return YES;
}

All components for core data have been implemented

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

    return managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
            return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];

    return managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                           stringByAppendingPathComponent: @"MyProjectName.sqlite"]];
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                             configuration:nil URL:storeUrl options:nil error:&error]) {
        /*Error for store creation should be handled in here*/
    }

    return persistentStoreCoordinator;
}

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

When I try to call a method in the detail view to store data, the program quits.

-(IBAction) addItem {
    Info *info = [NSEntityDescription insertNewObjectForEntityForName:@"Info" 
                                                   inManagedObjectContext:managedObjectContext];
    info.name = item.name;
}

item is the current object in the detail view, Info is the model class file. Do I miss something here?

Thanks!

Update:

The error message in the console is:

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

But I do have a Info.xcdatamodel file in the "Resources" folder, and entity name is "Info".


Did you call [self.managedObjectContext save:&error]?

Also, perhaps your bundle loading routine does not work correctly. Try loading the managedObjectContext like this:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ModelName" 
   withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] 
   initWithContentsOfURL:modelURL];    
return __managedObjectModel;


My guess would be that in your detail view controller you either aren't synthesizing managedObjectContext or you aren't initializing it when creating your detail view controller. Check that both of those are being done. If that doesn't solve the problem, please check the console output after the crash and post any relevant information there in an update to your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜