0

I can't seem to wrap my head around a good design that allows the user to create an object, save to the database and keeping track if that object is posted to the server or not.

Like in snapchat - while offline you can create a snap-object including picture and receivers, press send and it shows up in the list (table view) of snaps, but if the device is offline, the request times out or fails, it will show a red exclamation mark and you can tab the cell to retry.

What is a good approach for doing a similar design? Any ideas?

Thanks in advance!

Kleemann
  • 99
  • 7

2 Answers2

0

I had worked on similiar kind of app. The idea is how you structure your DB. In this case along with all other attribues in table add one more attribute say isSent which is a bool value. By default this should be false and should only be set to true when it is updated on server.

This will help in finding out which item are not synced to the server and send only those items whose isSent is false.

Now how your app will get to know once item is updated on server? For that your server should sent back the request you have sent with the success message for each request. You request can be an array of items or a single request depends on the requirement.

Azure sync table also works in the same fashion. When you first create an item it is stored in your offline DB with isSent as false then that request is sent to server, when server send the success isSent is updated to true.

Lets see an example of this: Suppose you are commenting in offline. Each comment will be considered as separate item. So when you are connected to internet these all request will be send and each item will have its unique Id to differentiate with other. Your request to upload to server will be:

[
    {
        "unique_id" : 1,
        "Comment" : "Hello"
    },
    {
        "unique-id" : 2,
        "Comment" : "How r u"
    }
]

Now response from server should be:

[
    {
        "unique_id" : 1,
        "Status" : "success"  
    },
    {
        "unique_id" : 1,
        "Status" : "failed"  
    }
]

Parse your response and based on Status update the isSent column of your DB accordingly.

Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
  • Thanks for your answer Arun. How would you get notified when the object is trying to send itself, and when it is failed or not? Also who is responsible for for sending the object? I've been reading up on the command pattern, will this be suitable for this problem? – Kleemann Apr 10 '16 at 05:42
  • Basically you need to observe for the internet connection or check at launch if there is any unsynced data. Developer has to manage of how and when to send.It should be a combination of patterns - Notification, singleton. Updated my answer with example scenario. – Arun Gupta Apr 10 '16 at 07:15
0

I would talke a look at services like Parse Server. If you do t know what it is, it's an open source server that you can mount (put) on a service like Heroku or Amazon AWS. When you save data to the server using the Parse SDK, you have the option to create a block which handles if an error occurred or if success equals true. You can take a look at YouTube videos on how to get Parse Server set up on, say, Heroku. From there, all of the old "Parse" videos will pretty much stay the same even though they are shutting down (hence the open source project). Also, you can use Reachability to detect if a user is online or offline -- again, you can find YouTube videos on how to do that.

Dan Levy
  • 3,931
  • 4
  • 28
  • 48
  • Thanks for your answer Dan! I'm not really looking for the easy way out here. I'm looking for a clean design approach to the problem, preferably a design pattern. – Kleemann Apr 10 '16 at 05:38