我必须将核心数据添加到现有项目中,但我遇到了 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);