1
votes

I'm trying add data to CoreData. This is my code.But I'm getting this error.What should I do in this case ?

That is my ViewController.h

@interface LoginViewController : UIViewController {
    NSManagedObjectContext *managedObjectContext;

}

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

And This is my .m file

       Person *person = [[Person alloc] initWithJSONString:string];
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObjectContext *object = [NSEntityDescription insertNewObjectForEntityForName:@"Users" inManagedObjectContext:context];
    [object setValue:person.name forKey:@"name"];
    [object setValue:person.id forKey:@"userId"];
    [object setValue:person.statu forKey:@"statu"];
    [object setValue:person.token forKey:@"token"];
    [object setValue:person.sonuc forKey:@"sonuc"];

- (NSManagedObjectContext *)managedObjectContext
{
    if (managedObjectContext != nil) return managedObjectContext;

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

        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return managedObjectContext;
}

This is error.

'+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Users''

Thanks for your help

2
Your entity name is likely to be User (singular), not Users. - Rog
not changed anything.I'm getting same error. - Murat Kaya
What does + entityForName:inManagedObjectContext: deliver? It is nil, too. (I expect that, just for checking.) Is managedObjectContext non-nil? - Amin Negm-Awad
does [self persistentStoreCoordinator] return nil? - Sulthan

2 Answers

1
votes

The heart of your problem is that you have not thought about where persistentStoreCoordinator comes from, and this object is never created.

Your check, if (coordinator != nil), is hiding this problem. Instead, you should aim to set things up so coordinator is never nil. Then the check can be removed or, even better, replaced with an assertion that this is the case: NSAssert(coordinator != nil, @"There is no persistent store coordinator.").

If the persistent store coordinator comes from outside this view controller, I recommend passing it as part of the view controller’s initialisation. If the view controller can set up the persistent store coordinator itself, either do this as part of its initialisation or load it lazily.

0
votes

It is better to use

NSManagedObjectContext *object = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([YourClass class]) inManagedObjectContext:context];

You don't need to use hardcoded string as it may have typing errors.

In your case it must be

NSManagedObjectContext *object = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Users class]) inManagedObjectContext:context];