One of the major problems that affects the operation of real-time location tracking is patchy mobile network connectivity. Our users trust our SDKs by plugging them into their apps that are out in the big bad world. We guarantee uninterrupted operation of the app regardless of network connectivity. Our SDKs are built to be offline-first. Collecting and handling location data on the smartphone is resilient against bad network. As a result it is considered to be the source of truth for all data including time – a crucial dimension for real-time location tracking.
In some cases the device time is incorrect which affects the integrity of the data collected from the device. In such cases the device data needs to be corrected to the true time.
Our friends at Instacart recently posted about TrueTime their solution to solve the same problem. We loved how they approached it – with their client side TrueTime libraries for Android and Swift. In this post we detail out our solution and weigh the pros-cons between the two solutions for others who are facing the same problem. If you haven’t already do check out the Instacart blog.
Any generic solution to this problem will need to rely on a source of true time. In Instacart’s solution true time is obtained by making a request to an NTP server and comparing it to the device time. In our solution we avoid the additional request and assume the time on our API server to be the true time. The time stamp data is corrected on every request by comparing the server time to the device time at the time of the request.
Client side: Add a new HTTP header with all requests that sends the device time at the time of the request. Let’s say this header is called
Server side: Override the deserialization and validation of the request body to check if any data time fields need to be corrected. To achieve this with the Django Rest Framework we created a new date time field called
TrueDateTimeField with a custom validation step.
TrueDateTimeField is available as a package on pypi which you can use in your applications with a simple pip install. To know more head to the Github repo.
Versus Instacart’s solution
Pros: Our solution is lightweight and platform independent on the client side. It just needs sending a new HTTP header in every request instead of adding new libraries.
Cons: Since the device time is captured before the request and compared to true time after the request the time taken in the request is ignored in the difference calculation. This means the correction can have an error of the order of 10-100s of milliseconds.
Correcting bad device time is one of the essential features in our offline-first SDKs. The offline first architecture brings reliability to our location tracking infrastructure and therefore to the features that you can build on top of it. Test it out by signing up and getting your API keys.