NSManagedObjectContext在Core Data应用程序(iOS)中返回nil

时间:2012-12-29 13:34:55

标签: objective-c ios core-data nsmanagedobject nsmanagedobjectcontext

每次按下按钮时我都会尝试将字符串保存到数据库中但是当我运行项目时,我会在控制台上找到它:'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Info''

参考数据模型,我创建了一个名为'Info'的实体的.xcdatamodeld,并在其中创建了一个名为'path'的属性,其类型为string。

我创造了三个功能。 “enterdata”通过调用“findData”检查名称是否可用。如果名称可用,则通过“newData”记录新数据,否则,它会查找不同的名称。

我一直在寻找类似的问题,我发现了this。它说de ManagedObjectContext必须传递给View Controller但我不明白它是什么意思。

这是我的.h代码:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

这是我的.m代码:

#import <CoreData/CoreData.h>

@synthesize managedObjectContext;

int  iSavedNum = 1;
bool bCanSave;

//Enter data
- (IBAction) enterdata:(id)sender {

    //Search if data is already registered
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@/info%i.png",docDir, iSavedNum];
    [self findData:path :@"path"];

    //If data is already saved, save it with new name.
    if (bCanSave == NO) {
        for (iSavedNum = 1; bCanSave == YES; iSavedNum++) {
            [self findData:path :@"path"];
            if (bCanSave == YES) {
                [self newData:path :@"path"];
            }
        }
    } else {
        [self newData:path :@"path"];
    }

}

//Input new data
- (void) newData:(NSString *)value:(NSString *)key {

    //Create ManagedObjectContext and ManagedObjectModel
    __0AppDelegate *appDelegate = (__0AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObjectModel *newRecord;

    //Put the data to the Entity
    NSString *entityName = @"Info";
    newRecord = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
    [newRecord setValue:value forKey:key];

    //Errors management and cheking
    NSError *error;
    [context save:&error];
    NSLog(@"Info Saved. Value: %@ Key: %@", value, key);

}

//Find Data
- (void) findData:(NSString *)valor:(NSString *)key {

    //Create ManagedObjectContext
    __0AppDelegate *appDelegate = (__0AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    //Call the Entity and make a request
    NSString *entityName = @"Info";
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    //Create predicate to call specific info
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(%@ = %@)", key, valor];
    [request setPredicate:pred];

    //Errors management and creation of an array with found info
    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];

    //Set if the name is avaliable or not
    if ([objects count] == 0) {
        bCanSave = YES;
    } else {
        bCanSave = NO;
    }
}

2 个答案:

答案 0 :(得分:4)

它准确地告诉你错误是什么:

  

nil不是合法的NSManagedObjectContext参数

这意味着在这一行:

newRecord = [NSEntityDescription insertNewObjectForEntityForName:entityName
                                          inManagedObjectContext:context];

变量contextnil。这意味着您的managedObjectContext方法无法正常运行。你没有显示这个,所以我们可以添加更多。

答案 1 :(得分:0)

application:didFinishLaunchingWithOptions:中的appDelegate

/*initiate the managed Object Context */  
CoreDataManager *coreDataManager = [CoreDataManager sharedDataManager];
    coreDataManager.managedObjectContext = self.managedObjectContext;

其中CoreDataManager是我的核心日期管理器,它明确包含所有核心数据保存,删除方法

yourClassObject.managedObjectContext = self.managedObjectContext;

所以上下文初始化