1
votes

I'm seeing a strange issue with the data in my app before and after a merge. I have an app that lets the user refresh so I have a separate context for that thread and any changes are then merged (as i believe I'm supposed to). I noticed that after the refresh, the data updates are correct (items added, removed, etc.) BUT my items are reordered. I can trace this to the different contexts, but have no idea why this would occur. Scenario is:

refresh and use deleteObject on background context save this triggers merge with notification querying the db on the background context shows the correct order querying the db on the main context (which should have changes merged to it) shows different order

if I've got things setup with the separate contexts and the merge, why would the data in the main context be different than the one in the background context post merge? i know this is weird, so any help is appreciated!

2
The order of the objects returned by a fetch request in undefined, unless you specify a sort descriptor. - Martin R
ok, strange thing is i use the same fetch request for each call, the only difference is the context, and they return different results. - skinsfan00atg
It doesn't matter, if you don't use sort descriptors, the order is undefined. - Tom Harrington
thanks guys, i understand that part, it was just so weird that i could call it a hundred times and it would come back in same order, but then my refresh was added and it was no longer the same order. i debugged and found that a CD save of the MOC fixed it. I'm still figuring out why, but just in case someone else runs into this, a save helped me - skinsfan00atg

2 Answers

1
votes

The order of the objects returned by a fetch request (without sort descriptor) is unspecified. If you need a well-defined order, you should add a sort descriptor to the fetch request.

0
votes

i debugged and found that a CD save of the MOC fixed it. I'm still figuring out why, but just in case someone else runs into this, a save helped me