Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found this question after looking at the same example in the Core Data Programming Guide. </p> <p>This is my solution: </p> <p>The key is that you are walking 2 arrays separately, one array contains all the employeeId strings that need to exist in Core Data, and the other contains Employee objects that already exist in Core Data, filtered by the employeeId strings. Both arrays have been sorted.</p> <p>So lets say we have the sorted employeeIds array containing strings:</p> <pre><code>@"10",@"11",@"12",@"15",@"20" </code></pre> <p>And that we have a matchingEmployees array containing 2 Employee objects with employeeId 10 and 15.</p> <p>We need to create new Employee objects for employees with employeeIDs 11,12 and 20, while potentially updating the attributes of employees 10 and 15 . So:</p> <pre><code>int i = 0; // employeeIds array index int j = 0; // matchingEmployees array index while ((i &lt; [employeeIds count]) &amp;&amp; (j &lt;= [matchingEmployees count])){ NSString *employeeId = [employeeIds objectAtIndex:i]; Employee *employee = nil; if ([matchingEmployees count]!=0) employee = [matchingEmployees objectAtIndex:j]; if (![employeeId isEqualToString:employee.employeeId]){ employee = //Insert new Employee entity into context employee.employeeId = employeeId; //Set any attributes for employee that do not change } else { //We matched employeeId to Employee so the next iteration //of this loop should check the next Employee object j++; } //Set any attributes for employee that change with each update i++; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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