I am working on iOS application which requires to maintain a clock timer session between two devices upon acceptance of both device users?
But I am not sure how this can be achieved without getting a flaw in timing on both devices?
I have already look on following ideas:
1)Maintain state of both devices with help of Webservices or push notification so that timer on both devices will start at the same time. But if network is not working properly this approach is getting failed and there is flaw in timer on both devices.
In addition, I also want to know how can I prevent application clock remain unchanged even after changing device date and time.
Any help over this would be appreciated.
Thanks in advance.
3 Answers
Answers 1
You can use ios-ntp to get network time and calculate time offset between local and network time on both devices. When you start timer on one device, you subtract the offset from the local timestamp and send the resulting network timestamp to another device. On another device you append the local offset to the network timestamp and get the timestamp of timer start in the local time.
You can use UIApplicationDelegate.applicationSignificantTimeChange(_ application: UIApplication) to monitor time changes on the device and recalculate the offset by updating network time.
Answers 2
Some thoughts:
keep track of your own time, be independent of the system clock. Only use the system clock to convert your time to user-wall-clock time when you need to display something.
a time-unit (like a second) on one device should equal a second on the other device, e.g. trust that the duration of it is equal on both devices)
network delay is a fundamental problem. Consider that there might always be an offset between the two devices. Maybe you can come up with a way that reveals the lag, or you could come up with a way where the absolute time is not relevant
Here is an idea: when device1 sends a sync-packet every 5 seconds, device2 should have this packet before it does whatever happens at second 6. So if the packet is not there yet in device2 at 5 seconds, it could wait for the packet to arrive before it continues to do whatever needs to be done next. When the packet arrives at 3 seconds, device2 should hold on to it and process it at second 5. Do this in a bi-directional way, and both apps are in sync within a 5 seconds time-frame. Depending on your requirements and the network, you have to decide what 'waiting' means and how often to send a sync-packet.
Answers 3
You can achieve by the webservice in which you just send flag to global clock,In this you just set flag for start and end time so by using this method you can easily synchronize the time between two ro more devices.
0 comments:
Post a Comment