Periodic sending small data to server in background mode

本小妞迷上赌 提交于 2019-12-25 06:44:13

问题


I am implementing a kind of tracking app and i need to spot my location once and send it once per 10 - 20 seconds (period value is important and can't be exceeded). To lower battery consumption i stop location updates. This works good in foreground, but how can i do it when app moved in background? I looked info about background fetch, but it hasn't got precise time for periodic sending data

How can i perform this task?


回答1:


You can start and Stop periodic location update while app is in background. To achieve this add class from given link for Location Update.

After that import LocationTracker.h in your AppDelegate.

Add Below code in your didFinishLaunchingWithOptions.

let locationTracker : LocationTracker  = LocationTracker();
locationTracker?.startLocationTracking();

In LocationTracker.m, you can set duration to restart update.Here i set 1 minute or 60 Seconds.

//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
                                                       selector:@selector(restartLocationUpdates)
                                                       userInfo:nil
                                                        repeats:NO];

You can also set duration time for fetch Locations. Here I fetch location for 10 Seconds.

self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
                                                selector:@selector(stopLocationDelayBy10Seconds)
                                                userInfo:nil
                                                 repeats:NO];


来源:https://stackoverflow.com/questions/34512007/periodic-sending-small-data-to-server-in-background-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!