Background Information
I am developing an iOS application that is connected to a server, and requests can be made from the device towards the server to synchronize the local database with the server's database. Changes can occur on either one of the databases. The application also has an offline feature, where users can modify the data without being connected to an internet connection, and only going online in order to synchronize the device's local database with the server's database, by sending new data and receiving updates from the server.
Updated with more background information: The data handled is a form that must be completed in a certain amount of time. The time it is started is stored as a property on the model, and using said property, the application shows the user how much time left before this form is locked.
Problem
While the device is offline, the user can manipulate the time settings, which would make the data received from this device inaccurate.
I have searched for a way to detect changes to the device time settings but according to this SO answer, that does not seem to be possible unless my application is running in the background, which Apple is very strict about, and does not allow unless the application had one of the background modes enabled, which are not applicable in the application.
Updated with more information: As I mentioned in the updated part of the background information above, the data is forms that could have a time limit. Therefore a user can start a form on one device, but not finish it there. They would then synchronize the data to the server, jump to another device and complete the same form.
Proposed Solution
The closest I've come to solving this problem is to get the difference between the server time and the device time when the application is installed (It is sure to be online the first time the application is opened in order to configure it for this device), and using this difference as reference whenever I want to store a time-related value.
If the server detects a difference in the time, it will reject all data coming from this device, which will force the device to revert the data, update the time difference, and only then will data be accepted from this device.
The problem is that while the device is offline, the user could change the time settings, perform changes on the data, and then returning the time settings to what they were earlier before going online. The server would then detect no changes in time settings and will accept the invalid data.
Update to add another proposed solution: To elaborate more on my note in the questions below, and the comments with @Krypton, the uptime of the device does not help much in my situation because of the same problem as the above solution.
Taking the device's uptime and comparing it to the server time is perfect, until the user reboots the device. It is not manually changeable by the user, however all it takes it one reboot to break everything, which would yield the same inaccurate data for me. Fixing it would require calculating the new difference, which would not help when the device is offline.
Questions
- Is there an unchangeable timestamp that can be used as reference when saving a time-related value, such that it cannot be affected by changes to device time from the user? An example of this would be the time since the device was first activated. (Note: The closest I have come to finding anything related is the uptime of the device, but that doesn't help much in my situation)
- Is there a way any way to know the current time without the need to go online whenever a time-related value is going to be saved?
- Is there a way to fix my proposed solution, or any better solution?