NSManagedObjectContext = [NSEntityDescription ...]后的SIGABRT

时间:2012-02-11 10:05:13

标签: iphone ios core-data sigabrt

我正在尝试做一些与正在做的事情here非常相似的事情,但由于没有给出真正的答案,我想看看是否有人可以帮我解决我的特定问题。

我只是想将Core Data添加到我拥有的现有应用中。 以下是我添加到各自文件的内容。 我还概述了当我的应用程序加载时我收到SIGABRT的位置。 我已经验证“loadData”中的上下文变量不是NULL。

AppDelegate.h

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : NSObject <UIApplicationDelegate> {
}

...

@property (readonly, retain, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, retain, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, retain, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;     
-(void)loadData;
@end

AppDelegate.m

#import "NewModel.h"

@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
...

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

    __managedObjectContext = [[NSManagedObjectContext alloc] init];
    __managedObjectModel = [[NSManagedObjectModel alloc] init];
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] init];
    [__managedObjectContext setPersistentStoreCoordinator:__persistentStoreCoordinator];        
    [self loadData];
}

- (NSManagedObjectContext *)managedObjectContext {
    if (__managedObjectContext) {
        return __managedObjectContext;
        }

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


-(void)loadData
{
    NSManagedObjectContext *context = [self managedObjectContext];

    NSManagedObjectModel *newObj; // Tried NewModel = *newObj; thinking that may resolve 
                                  //         the issue, didn't work though
    newObj = [NSEntityDescription
               insertNewObjectForEntityForName:@"NewModel"
               inManagedObjectContext:context]; --> SIGABRT WHEN TRYING TO EXECUTE THIS
    [newTeam setValue:@"value" forKey:@"modelValue"];
    NSError *error;
    [context save:&error];
}

NewModel.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface NewModel : NSManagedObject    
@property (nonatomic, retain) NSString *  modelvalue;
@end

NewModel.m

#import "NewModel.h"

@implementation NewModel
@dynamic modelValue;
@end

4 个答案:

答案 0 :(得分:1)

我上面的代码的问题是我没有正确地初始化我的persistentStoreCoordinator或managedObjectModel。以下是曾经添加的两个功能,让我解决了这个问题。

    /**
     Returns the managed object model for the application.
     If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
     */

- (NSManagedObjectModel *)managedObjectModel {

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

    NSString *path = [[NSBundle mainBundle] pathForResource:@"LocalDatabase" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    //managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];    
    return managedObjectModel;
    }


    /**
     Returns the persistent store coordinator for the application.
     If the coordinator doesn't already exist, it is created and the application's store added to it.
     */
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
        if (persistentStoreCoordinator != nil) {
            return persistentStoreCoordinator;
        }

        NSString *storePath = [ [self applicationDocumentsDirectory] stringByAppendingPathComponent:@"LocalDatabase.db"];
        NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

        // Put down default db if it doesn't already exist
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if (![fileManager fileExistsAtPath:storePath]) {
            NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"LocalDatabase" ofType:@"sqlite"];
            if (defaultStorePath) {
                [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
            }
        }

        NSError *error = nil;
        persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }           
        return persistentStoreCoordinator;
    }

答案 1 :(得分:0)

哪一个,newmodel或teammodel?

 @interface NewModel : NSManagedObject    
 @implementation TeamModel

答案 2 :(得分:0)

你有没有写过这个getter方法:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    return __persistentStoreCoordinator;
}

你在这一行上调用它,但我没有看到它在任何地方定义。

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
    return nil;
}

答案 3 :(得分:0)

您是否创建了数据模型,启动了应用程序,然后更改了数据模型,然后再次尝试启动应用程序?如果是这样,您的应用程序将崩溃,因为持久性存储与数据模型不兼容。您需要删除自己的应用并重新安装,而不是安装“在顶部”。

另外......请设置断点并确保'上下文'不是零。我假设你有@synthesize managedObjectContext = __managedObjectContext; ??