Search code examples
iosobjective-ccore-datansmanagedobjectnsmanagedobjectcontext

Creating instance for NSManagedObject but not saving it into context


I am creating an instance for Employee object by following code snippet

NSEntityDescription *entity = [NSEntityDescription entityForName: Employee
                                          inManagedObjectContext:self.managedObjectContext];
Employee *blioDownloadInfo = [[Employee alloc] initWithEntity:entity
                                               insertIntoManagedObjectContext:nil];

I have assign id and name for employee object but haven't saved the managedObjectContext.

I am doing some other operation in the context with different entity and saved the context.In this scenario I have 2 questions.

1. First of all, Does the employee instance present in the context?

2. Will the context lose the employee object?


Solution

  • The entity instance doesn't exist in the context because you haven't added it, this is because you passed nil in insertIntoManagedObjectContext:nil.

    So the context can't lose it. You could lose it if you discard the reference to it.

    When you're ready, call insertObject: and then any saves you make on the context will involve the entity instance.