Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - core data - save unsuccessful after using remove methods in NSManagedObject Subclasses
    primarykey
    data
    text
    <p><strong>Overview:</strong></p> <ol> <li>I remove an NSManagedObject using the remove methods provided in the autogenerated NSManagedObject subclass </li> <li>Then I attempt to save the database</li> <li>The database save is unsuccessful.</li> </ol> <p>.</p> <pre><code> - (void)removeEmployees:(NSSet *)values; //Auto generated method in Department.h </code></pre> <p>However adding of an NSManagedObject using the add method allows the database to be saved successfully</p> <pre><code> - (void)addEmployeesObject:(Employee *)value; //Auto generated method in Department.h - works fine </code></pre> <p><strong>Note:</strong> I am using NSManagedObject subclasses generated by XCode.</p> <p><strong>Entity Details:</strong></p> <ul> <li>I have 2 tables (entities) namely "Employee" and "Department"</li> <li>One department can contain multiple employees</li> <li>One employee can only belong to one department</li> </ul> <p><strong>Relationship Details:</strong></p> <ul> <li>Relationship from an "Employee" to "Department" is a One to One relationship and is called "whichDepartment". Delete rule is Nullify</li> <li>Relationship from an "Department" to "Employee" is a Many to One relationship and is called "employees". Delete rule is Cascade</li> </ul> <p><strong>Problem:</strong></p> <p>"Department" class has a method called as mentioned below, after I use this method, the database save is not successful</p> <pre><code>(void)removeEmployees:(NSSet *)values; //Auto generated method in Department.h //does not work </code></pre> <p><strong>Code used to Delete:</strong></p> <pre><code>- (void) removeEmployeesHavingAgeAsZeroWithDepartment: (Department*) department { NSMutableSet *employeesSetToBeRemoved = [[NSMutableSet alloc] init]; for(Employees *currentEmployee in department.employees) { if(currentEmployee.age == 0) { [employeesSetToBeRemoved addObject:currentEmployee]; } } [department removeEmployees:employeesSetToBeRemoved]; //causing the problem } </code></pre> <p><strong>Code used to save the database</strong></p> <pre><code>[self.database saveToURL:self.database.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {NSLog(@"DB saved");}]; </code></pre> <p><strong>Questions:</strong></p> <ol> <li>Why is it that the saving the Database not successful after using the remove methods?</li> <li>Do I need to implement this method ? currently i don't see any implementation for this method, I can only find it in the header file.</li> <li>Are my relationships (inverse, delete rules, etc) set up correctly ?</li> </ol> <p><strong>Answer:</strong></p> <ul> <li>Pls refer to Jody's answer and comments (May 17 at 4:39), it is explained well as how to debug the problem and find the root cause</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload