Multiple PersistentStoreCoordinator

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

My first question.

If you have 10 items on list 10 boxes.

If i have app on my phone and you have one on yours

We both are offline

Can i change the same box as you ?

I mean i choose box 1 and one item no 2 check undone (unpack)

You choose box 1 and check item no 5 as undone

So what now when i connect to internet i will send one change undone item no 2 and rest imet check as done in box no 1

And after 3 minutes you send undone item nr 5 and done rest as done in box no 1


how you resolve this ?

sorry for delay, I did not received any notification of the activity 😕


ok the list view contains boxes and the details view is for displaying the box items (articles)


yes, we can be both offline changing the same box


usually that should not happens since the staff are trained to work on the own box


not sure what you means by undone ?


Right now the solution that I choose for my concurrency data stack is as follow:


Main Context (Main Queue)

Background Context (Private Queue, Child of Main) for parsing my JSON requests and processing offline requests

Worker Context for the Box Items view (Main Queue, Child of Main)


so if I am processing changes in the background they will be push to the main context and by the same time if I am working in a box, these changes won't be affected by the background context


thanks

Multiple PersistentStoreCoordinator
 
 
Q