我正在尝试做与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];
}

新模型
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

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

新模型
#import "NewModel.h"

@implementation NewModel
@dynamic modelValue;
@end

最佳答案

上面我的代码的问题是我没有正确正确地初始化我的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;
    }

关于iphone - NSManagedObjectContext = [NSEntityDescription…]之后的SIGABRT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9239349/

10-11 16:36