CoreData:未创建managedObjectContext

时间:2013-11-04 18:41:20

标签: cocoa-touch core-data

我必须将核心数据添加到现有项目中,但我遇到了managedObjectContext ...

的问题 在prefix.pch中的

我有这个:

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
#endif
...

在我的appDelegate.h中我有这个:

#import <UIKit/UIKit.h>
#import "AppViewController.h"

#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDFileLogger.h"
#import "DDTTYLogger.h"
#import "KIP_LogManager.h"


@interface AppDelegate : UIResponder <UIApplicationDelegate> {

    DDFileLogger* fileLogger;

    /*coredata*/
    NSManagedObjectModel* managedObjectModel;
    NSManagedObjectContext* managedObjectContext;
    NSPersistentStoreCoordinator* persistentStoreCoordinator;

}

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


- (void)setupLogging;

- (NSString *)applicationDocumentsDirectory;
在appDelegate.m中:

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

...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //establish lumberjack logging
    [self setupLogging];

    DDLogVerbose(@"\n\n*********************\nNEW LOG SESSION\n**********************\n\n");

    //set root view controller
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[AppViewController alloc] init];
    self.window.rootViewController = self.viewController;

    self.viewController.managedObjectContext = _managedObjectContext;

    return YES;
}

...

    #pragma mark - CoreData Stack

    - (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.
    // If the model doesn't already exist, it is created from the application's model.
    - (NSManagedObjectModel *)managedObjectModel
    {
        if (_managedObjectModel != nil) {
            return _managedObjectModel;
        }
        NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SPI_PAC" withExtension:@"momd"];
        _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
        return _managedObjectModel;
    }

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

        if (persistentStoreCoordinator != nil) {
            return persistentStoreCoordinator;
        }

        NSMutableString* strDocsDirector = [[NSMutableString alloc] initWithString:[self applicationDocumentsDirectory]];
        [strDocsDirector appendString:@"/SPI_PAC.sqlite"];

        NSURL* storeURL = [NSURL URLWithString:strDocsDirector];

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

在视图控制器中,viewDidLoad我正在记录传递的managedObjectContext并返回null。显然我错过了什么,但不确定是什么?

在viewcontroller.h中我有:

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

并在viewcontroller.m中:

@synthesize managedObjectContext = managedObjectContext;
...
 DDLogVerbose(@"%@", managedObjectContext);

2 个答案:

答案 0 :(得分:1)

这是因为没有调用MOC属性的重写getter - 你正在访问实例变量。

变化:

self.viewController.managedObjectContext = _managedObjectContext;

要:

self.viewController.managedObjectContext = self.managedObjectContext;

答案 1 :(得分:0)

经过一段时间(整整一天)在谷歌周围徘徊,在这里,我找到了一个有效的解决方案。我在这里找到了解决方案:

iOS Core Data App Delegate?

我自己实现了一切,而不是使用Apple的固定核心数据堆栈。