I’m trying to follow the steps found here on comparing two arrays, and knowing when to create a new object, but I just don’t understand how it works:
You end up with two sorted arrays—one with the employee IDs passed
into the fetch request, and one with the managed objects that matched
them. To process them, you walk the sorted lists following these
steps:
Get the next ID and Employee. If the ID doesn't match the Employee ID, create a new Employee for that ID.
Get the next Employee: if the IDs match, move to the next ID and Employee.
Regardless of how many IDs you pass in, you only execute a single
fetch, and the rest is just walking the result set.
Basically what’s happening is I have an array of object ids from an external source, and the client system only has a subset of the objects represented by these ids. I need to figure out which objects I already have, and if I don’t have them, create them, one by one.
I don’t understand how this works. I’m having trouble translating this to code:
for (int i =0;i<ids.count;i++) {
currentId = [ids objectAtIndex:i];
currentObject = [objects objectAtIndex:i];
if(currentObject.id != currentId) {
//create new object
}
//"get the next employee"
//uh what?
nextEmployee = [objects objectAtIndex:i+1]; //?
if(nextEmployee.id == currentId) {
//"move on to the next id"
continue;
}
}
I don’t see how that would work? What am I missing?
Try something like this:
Hope this helps!