Im am developing a webservice for an App that I am writing. I want to make the App offline accessible.
I made that webservice so that if you request JSON from the webservice you can give a date:
/color/colors/date/2014-03-01T12:00/
If you don't give the date you will get everything that is inside the database and that is active. If you give the date you will only get everything that is updated after that date.
Now my problem is that if I remove something from the webservice of from the App then it will not be synced and the other devices will never know that it is removed.
I could work with a field where I say that a record is removed but then I need to keep every record and I can't delete any record.
Is there a better way to do the syncing? Or what is the best way?
I think there is no possible way to detect the deleted entry's change, unless you send the information that the entry is deleted. The best practise you set a field in the table with integer type, then you can set this value on updates. You don't have to set it only 1 or 0, you can use bigger numbers (for example I used 30 on that entries, that I deleted on 20th march , on this day was a big code logic change. after that i knew when the status integer is 30, then i deleted this row after that date.) It may be a silly example, but you can implement your own logic.
It depends on how important it is to update the rest of the devices when a change is made.
If it is critical, then it would be worth implementing push notifications or something similar to each device to let them know about the updated situation. Otherwise, you would simply have the other devices poll the server to check for changes on their own accord, and the frequency or the trigger of this poll would depend again on how critical it was that they get an update. Maybes they only need updating when they visit a certain activity, so in that case you would only poll when you reach the onResume() event of that activity
Update
If you don't need to keep a history of the deleted record, then why can't you just delete it, and then when the rest of the devices update, you clear all and download a fresh set? If that is too intensive, you would NEED to have a reference to the id, which you could do in a table or use a special value in the field (like null, 0 or -1) to mark it. Otherwise there would be no way to reference it
Related
I have a data field and time field ,user Enter data and time
After Writing data on firebase realtime Database I want to reset data field and Time Field to 0 after The specifed time in Time Field
I read about cloud function but I don't know to solve my problem
I'm new to firebase and it's cloud function Please help...
Data field will contain somedata
Time Field Will Contain data like 20 ,30 or 40 which represent time in mins
so after writing both to database
say if time field contain 20 ,so after 20 min to writing data in database, it should be reset to 0
Thanks in advance
The scenario you describe could be achieved in two ways. In my opinion, one can be cheaper and easier (no function involved but need to add some logic and a query to data on client side) if you could implement it which depends on your case.
Not the cheapest one: Schedule (cron type jobs) to scan your database regularly and check for the specified time and reset your data if condition is met. And this requires you to use Firebase functions and cron type services to configure it.
The cheaper way (although dependent on your case) in my mind would be to set your logic client side, so whenever a user is navigating to that data , check for the time and if condition is met reset the data client side without client noticing. This way, you don't need to set up functions, and you are not performing anything as long as no client has gone there (wherever that the data your mentioned is used in your app) Just keep in mind that this depends on whether you can have such scenario or your data has to be update regardless of users interaction with it. I have managed to redesign stuff whenever I have come across a case that needs periodic updates, etc.
More info on second option:
Imagine your users are checking for an order which can expire after some time. Your intention is to reset data when expiry time has arrived. Instead of resetting data via functions, etc, you can write logic so that whenever a client queries the orders, you check for expiry time and if expired, you perform what has to be done there and then, in addition to making sure your client won't see the expired order. Hope this makes it more clear. It's sort of a passive way of updating your data in db.
I'm kind of confused about how to do this. I have string values in the realtime database in firebase console. They change when the app is running because the code is causing the values to change, but when the application is stopped, the values won't change anymore. I want the values to change even when the application is running or not. So for example, a counter variable in the database will go up every minute forever as long as the data is there.
From your explanation this seems to be an issue which can be easily solved by keeping the app running in background. For this purpose look here.
Quoting the answer from above link
A very simple answer for your problem is to use Service. It will allow you to perform variety of tasks while being in background and is your best bet for sending your location to server silently.
Read this answer for help.
Although both the answers are related to location update but they can be easily extended to fulfill your requirements.
Hint: The above quoted answers are sending the data to a server at frequent intervals. Send your data to Firebase server when the data-field is changed.
Hope this answers your query. Provide some code I might help with that too, if I get time.
So I have discovered that an app can be updated whilst the app is in mid-use. My app records a workout and stores values into a local DB before uploading the workout.
During the last update, we altered the databases, adding new tables and columns. A certain number of users had their app update mid-workout, corrupting the database. While that can be fixed after the fact, it doesn't change the fact that the user has lost that workout. I've been searching but haven't yet found something that would allow for the app to wait on the update until it isn't in use.
Any suggestions are welcome.
Thank you for your time.
You can't stop the update, but you can make sure, there is no corruption, and there is no data lost: Before Update, the application's onPause() callback will be called - time to make sure, the App is in a recoverable state. This might include storing the momentary working set to some temp storage. After update, the app will receive onResume(), where you can load the working set back.
I am writing a backup application and need to know which records in the contacts database have been updated, so that I can backup only those records. I have looked at the documentation and it seems that there is a "DIRTY" constant field in ContactsContract.RawContacts class, which is supposed to be set to "1", for the rows that are updated. But it is not clear to me as to when will this field get cleared to "0". Can someone provide me example code on how to use this? Can this field be used to determine if a contact has been added or updated?
If this is not the correct way to achieve what I am trying to do can anyone suggest me another way. I am also aware that I can use the RegisterContentObserver() call to identify whenever there is a change in the Contacts database but this will require my application to be running always in the background, which is way too expensive and I do not want to do that.
If there is anyway to extract the timestamp when the various contacts have been added or updated that would be perfect too, but I cannot find how to do that.
Any help is very much appreciated.
I have looked at the documentation and it seems that there is a "DIRTY" constant field in ContactsContract.RawContacts class, which is supposed to be set to "1", for the rows that are updated. But it is not clear to me as to when will this field get cleared to "0".
In my experience, whenever there is a 'dirty' indicator of some sort, it is the responsibility of the backup/sync app to reset it once the data has been successfully committed during a backup/sync operation.
This can cause problems, however, when more than one application is used - the first one run at any time after data has been updated will reset the flag and the next one run wont find anything to backup/sync.
In this case if you require that a user is able to use a 'sync' app (for example) but you also want to have a 'backup' operation then registering a ContentObserver would serve a better purpose and there's no reason why this should be 'expensive' on resources if implemented correctly.
EDIT: Although there is no 'timestamp' there is a 'VERSION' field which is updated (which is when 'DIRTY' is set). If you backup this field, you could simply leave the 'DIRTY' flag set and compare current VERSION in the contacts DB with your most recent backup.
I have an app fetch data from internet, for better performance and bandwidth, I need to implement a cache layer.
There are two different data coming from the internet, one is changing every one hour and another one does not change basically. So for the first type of data, I need to implement an expire policy to make it self deleted after it was created for 1 hour, and when user request that data, I will check the storage first and then goto internet if nothing found.
I thought about using a SharedPrefrence or SQLDatabase to store the json data or serialized object string.
My question is:
1) What should I use, SharedPrefrence or SQLDatabase or anything else, a piece of data is not big but there are maybe many instances of that data.
2) How to implement that expire system.
I'd use SQLite for storage + have a last invalidated timestamp in Application subclass.
It would get compared against System.currentTimeMillis() on each data access call to decide whether a new set should be fetched. Have a thin proxy layer for that.
You can use the HTTP HEAD method to check the modification date on the server and see if you really need to fetch new data. Every time the application launches, and at intervals while it is running, query the server to see if the data has changed. This assumes the data is cached on the server and not dynamically generated at every request or dependent on which client makes the request.
Thus you need to store the data and date for each item. SharedPreferences should suffice if the data is a string of moderate length and there is less that a few kilobytes total. If there is a known upper bound on the length of the data then use a database, otherwise you could use plain files. SharedPreferences writes an xml file every time you commit.
You can create a Thread with a long sleep interval to do the periodic checks, or create a Handler and use postDelayed or similar to spawn a checking thread. Check items as often as your maximum for stale data. If you check every 10 minutes, you allow up to 10 minute old data, with an average of half that. Checking at launch will make things appear up to date in most cases anyway.
If all the items expire at once, then you only need to check the date of one item to know they should all be refreshed. If not, you could try to use conditional GET instead of checking the HEAD of each item.