MonkeyBlue
MonkeyBlue

Reputation: 2234

Core data posting data to web service preventing duplicate posts?

This is perhaps a simple one but I have been running around in circles for a few hours trying to work out the best way to do this.

Essentially my app allows a user to create a post entry, which is then saved into core data and then posted to a web service if the Internet is available during this time the posting to the web service is done in a background thread allowing the user to carry on working.

The records are flagged SendToWebService = 1

My issue now is that the user can view a list of the entries they made in the app and select to re post it to the web service if it has not already happened, however this is causing duplicate posts as the previous background thread is still working on posting the entry as it is uploading an image or something big.

Any suggestions on how to best handle this?

Thanks

Upvotes: 0

Views: 154

Answers (3)

danypata
danypata

Reputation: 10175

In case the user is viewing the list of entries from the database than the easiest way wold be:

  1. When post event happen, save the post in database as sent to server and start the background thread.
  2. When the thread completes the run, check if the upload failed mark the entry in db as not uploaded, if it was with success do nothing.

By saving the state of the upload in db, the state will persist even if the user changes the screen or closes the app.

Upvotes: 0

Jonathan
Jonathan

Reputation: 2738

how about this, set SendToWebService=1 for the post that you are currently sending, if it goes through leave it 1 or delete the entry (depending on your implementation) but for some reason if it fails to post, set your SendToWebService back to 0. so when a post is in progress of being sent, it would appear as if its sent.

But if you want to be more transparent about the functionality, create another Boolean called InProgress or something and then turn it 1 when you are sending a request and do not let user post posts who have InProgress True and you can show which ones are in process of being sent in the UI as well, if it gets posted, turn your SendToWebService=1 , if not then Turn your InProgress again to 0

Hope that helped

Upvotes: 1

sbarow
sbarow

Reputation: 2819

I would suggest having 3 flags for uploads in your core data object.

   0 => upload failed,
   1 => currently uploading, 
   2 => upload complete.

As soon as the user selects to upload the post set the flag to currently uploading, in which case you set the update button to a spinner or something. When it completes, either failed or finished then change the upload button to done or re-upload depending on the flag.

This seems like the obvious answer hope I understood your question correctly.

Upvotes: 1

Related Questions