0

I'm developing an App where I store my data in a DB online using HTTP POSTO and GET. I need to implement some reliability to my software, so if the user presses the button, and there is no connection, the data should be stored in something (file? sqlite?) and then when the connection is again on, send the HTTP request to send data.

Any advices or pieces of code to show me how to do this? Thanks.

step
  • 2,254
  • 2
  • 23
  • 45
xcsob
  • 63
  • 1
  • 9

1 Answers1

0

Sounds good and pretty forward for me. Just go.

You use a local sqlite db as "cache". To keep it simple, do not implement any logic about that into your apps normal code. Just use the local db. Then, separately, you code a synchronizer. That one checks for the online connection and synchronizes the the local sqlite database with a remote database, maybe mysql.

This should be perfectly fine for all applications that to not require immediate exchange of the data with other processes all the time.

There is one catch, though: the low performance of sqlite on bigger data sets. That is an issue with all single file database solutions. So this approach probably is only valid for small data sets in total, or if you can reduce the usage of the local database to only a part of the total data, maybe only the time critical stuff.

Another workaround might be to use joins over two separate databases, the local and the remote one. But such things really boost the complexity of code, so think thrice if that really is required.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks for answering. I can't use the sqlite for caching always, because my app needs some immediate updates on the db online. On other functions, there would be no problem if there isn't connection, but in a particular function i need the reliability, because the user could probabiliy loose his data. So the solution i thought is like yours, creating a sqlite DB ( very small data to manage ) where I put my data and if there is connection, i save my data there and then where is online, store to mysql db. – xcsob Apr 11 '15 at 15:47
  • That would be a classical cache approach. – arkascha Apr 11 '15 at 15:51
  • Another question, i create my sqlite db on the first activity (to store locally user information). I create tables in on create method of database handler, so the tables are created in the first activity. Now how do I can get the same instance of DB in an another activity? – xcsob Apr 11 '15 at 15:53
  • You can specify which file to use. If the file already exists, the sqlite implementations use the existing database. But I doubt that something like sqlite is a good choice for a normal cache strategy. Do you really need to cache such complex data locally? – arkascha Apr 11 '15 at 16:11
  • my issue is that i cant let user loose his data in a function, if the connection is off. now my question is, if I instantiate the db handler on the first activity, how can I get the same istance of it in another activity? – xcsob Apr 11 '15 at 16:17
  • You stated all that before. Why do you repeat it? – arkascha Apr 11 '15 at 16:19
  • I create tables in onCreate method of the DatabaseHelper. So when I call the constructor in the first activity, it creates tables. When I want to use the DB in another activity, I do I get the istance of it? Should I use the singleton pattern to get only one istance of DB? Or I call anyway the constructor, and no matter if it recreates tables? – xcsob Apr 13 '15 at 07:43
  • Sorry, I have no idea about that DatabaseHelper of yours. This appears to be a completely separate topic. Reattaching to an existing database certainly is possible, I suggest you read the documentation? There is nothing I can say here, since, as said, I have no idea about the utilities you use. – arkascha Apr 13 '15 at 07:55
  • Sir., I read documentation. I extended SQLiteOpenHelper, and then create a DBHandler. In my onCreate method i create tables. I solved my issue, when I call the constructor, it calls itself the oncreate method. Whenever I call the constructor, it doesn't recreate the tables. So I suppose that there is a singleton pattern implemented in the base class of SQLiteOpenHelper, so that when we make a recall of the constructor it gives us only the referement of the instance. – xcsob Apr 13 '15 at 10:06
  • Sorry again, I still have no idea what Helper you are talking about. You never even mentioned it in your question. but as said: this is a completely separate aspect, if I get you right. Please create a separate question for this. In that question show your code, how should we help with that code if you don't show it? – arkascha Apr 13 '15 at 10:25