Get ObjectManagedContext from Delegate

I'm need to get de ObjectManagedContext from my AppDelegate, but when i tried it don't work... i dont know why... i followed a mount of tutorials but it not work.... this is my code:

AppDelegate.h

#import <UIKit/UIKit.h>
@class StopWalletViewController;

@interface StopWalletAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (strong, nonatomic) StopWalletViewController *viewController;

@property (strong,nonatomic) UINavigationController *navigationController;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

AppDelegate.m

#import "StopWalletAppDelegate.h"
#import "StopWalletViewController.h"
@implementation StopWalletAppDelegate

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize navigationController = _navigationController;

//Code Auto Generated

// Returns the managed object context for the application.
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

// Returns the managed object model for the application.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"StopWallet" withExtension:@"momd"];

    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return _managedObjectModel;    
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"StopWallet.sqlite"];
    NSError *error = nil;

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        /*

         Replace this implementation with code to handle the error appropriately.
         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
         Typical reasons for an error here include:

         * The persistent store is not accessible;

         * The schema for the persistent store is incompatible with current managed object model.

         Check the error message to determine what the actual problem was.

         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.



         If you encounter schema incompatibility errors during development, you can reduce their frequency by:

         * Simply deleting the existing store:

         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:

         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
         */

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        abort();

    }
    return _persistentStoreCoordinator;
}

@end

controller.m

#import "ExpenseViewController.h"
#import "Expense.h"
#import "ExpenseLocation.h"
#import "StopWalletAppDelegate.h"

@implementation ExpenseViewController

@synthesize editTextDate;
@synthesize editTextLocation;
@synthesize editTextValue;
@synthesize imageViewReceipt;
@synthesize managedObjectContext;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title=[NSString stringWithFormat:@"Expense"];
    UIDatePicker *datePicker;
    datePicker = [[UIDatePicker alloc]init];
    [datePicker setDate:[NSDate date]];

    [datePicker setDatePickerMode:UIDatePickerModeDate];
    [datePicker removeTarget:self action:nil forControlEvents:UIControlEventValueChanged];
    [datePicker addTarget:self action:@selector(updateTextFieldDate:) forControlEvents:UIControlEventValueChanged];
    [editTextDate setInputView:datePicker];

    StopWalletAppDelegate *appDelegate = (StopWalletAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext* context = appDelegate.managedObjectContext;

    managedObjectContext=context;

}

@end

This code are compiling but not work on runtime.... the error: NSInvalidArgumentException, reason:'Cannot create an NSPersistentStoreCoordinator with a nil model'

Can anyone help me?

Thanks...

Answers


The problem should be in - (NSManagedObjectModel *)managedObjectModel . Check the modelURL

- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
    return _managedObjectModel;
}
 // Give proper name of your model here
  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FailedBankCD" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}   

Try to assing managedObject context directly to the appdelegate. See bold line below. Could you also share your ExpenseViewController.h file. Just curious to see how you defined your managedObjectContext property.

- (void)viewDidLoad
    {
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
        self.navigationItem.title=[NSString stringWithFormat:@"Expense"];
        UIDatePicker *datePicker;
        datePicker = [[UIDatePicker alloc]init];
        [datePicker setDate:[NSDate date]];

        [datePicker setDatePickerMode:UIDatePickerModeDate];
        [datePicker removeTarget:self action:nil forControlEvents:UIControlEventValueChanged];
        [datePicker addTarget:self action:@selector(updateTextFieldDate:) forControlEvents:UIControlEventValueChanged];
        [editTextDate setInputView:datePicker];

        StopWalletAppDelegate *appDelegate = (StopWalletAppDelegate *)[[UIApplication sharedApplication] delegate];
         **self.managedObjectContext= = appDelegate.managedObjectContext;**

    }

Need Your Help

How to retrive binary version number from binary files

c eclipse binary version

I am using Eclipse IDE for developing the c programs. Through terminal I can see the file info using file filename .But I need to print only the version number of a binary file through the same c...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.