Hi folks, here is my problem:
App Detail
I have a sample app where a store create & pack some boxes and then send it to the warehouse
CoreData Model
Let's say my core data model has only 2 entities, Box and BoxItem with a relationship between the 2 (one to many)
App views
Let's say there is only 2 views, a view for displaying all the boxes and a view for displaying the box itself (it's items)
Views:
Boxes View (UITableViewController with a pull to refresh)
Box Items (UITableViewController, insert, change, delete article with 2 operations save the box or cancel to go back to boxes view)
Business Requirement
The business requirement is to be able to work offline, there come the complexity
I have created a third entity, "OfflineRequest", everytime the user save a box and we are offline, I will prepare a request and add it to this entity
I used the reachability API to know when the WIFI is on / off, as soon as I get the notification I will fetch this entity and if it's not empty I will send the request to the server using NSURLSession, that works fine
Problem
Let's say there is 10 boxes in the list, you choose box 1 to modify it and you are offline, you change the content of the box hit save and come back to the list box,
The wifi comes back and you do a pull to refresh, I need to be sure here that if I received the box list before sending my offine request that the Box 1 don't get updated, because it will produce a desychronization
Solution
I found 2 ways of doing this
1. When receiving the boxes from the server, before updating my CoreData entities Box and BoxItems I could check if I have an entry for that particular box in my OfflineRequest entity, if it's the case, don't update (that means the offline request have not yet been sent to the server)
2. The other way is to have a flag in my Box entity saying if there is offline changes that have been submitted or not
The problem with solution 2:
Let's say your are currently displaying Box 1 and in background I am sending the offline request (because the wifi came back and I need to process the offline data) to the server which will saved the persistent coordinator
by saving with the same context or a multi hierachy context, it will save what I am actually working on the BoxItems, let's say the user made somes changes and he click on cancel because he don't want to apply these changes, if in the meantimes I have save in the background my offline request with the same context, I won't know what I have changed or not, the managed object context hasChanged method will return nothing
What you be the best design for handling this
thanks
alex