简体   繁体   English

具有后台支持设计问题的位置感知iOS应用

[英]Location-aware iOS app with background support design issue

I have location services enabled for updating locations when app in background, and I also listen for location updates when app in foreground, displaying a map. 我启用了位置服务,可以在应用程序处于后台时更新位置,并且当应用程序处于前台时,我还会监听位置更新,并显示地图。 What should be the best way to design this scenario in iOS? 在iOS中设计此场景的最佳方法是什么? I've thought about some options: 我考虑过一些选择:

1) Having an instance of a class with a locationManager member that is its delegate itself. 1)具有一个具有locationManager成员的类的实例,该成员本身就是其委托。 Then, in the body of the didUpdateToLocation delegate method, something like: 然后,在didUpdateToLocation委托方法的主体中,类似于:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   if (!background) {
      // Perform some processing and notify the view controller which displays the map
      // by means of Notification Center
   }
   else {
      appDelegate.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
         [[UIApplication sharedApplication] endBackgroundTask:appDelegate.bgTask];
         appDelegate.bgTask = UIBackgroundTaskInvalid;
      }];

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         // Perform some data processing 

         // Close background task
         if (appDelegate.bgTask != UIBackgroundTaskInvalid) {
            [[UIApplication sharedApplication] endBackgroundTask:appDelegate.bgTask];
            appDelegate.bgTask = UIBackgroundTaskInvalid;
         }
      });
   }
}

(Note: I'm not sure if having location services enabled as background service, it is needed to perform the processing of locations as if it were a finite background task...). (注意:我不确定是否启用了位置服务作为后台服务,需要执行位置处理,就好像它是一个有限的后台任务......)。 The instance of such class could be a member of AppDelegate , and start listening for locations when entering background or calling the instance from the viewModel displaying the map. 此类的实例可以是AppDelegate的成员,并在输入背景或从显示地图的viewModel调用实例时开始侦听位置。 So, AppDelegate would have a reference to the instance managing the locationManager , and the map viewController would have a reference to AppDelegate , or communicate also by means of Notification Center. 因此, AppDelegate将具有对管理locationManager的实例的引用,并且map viewController将具有对AppDelegate的引用,或者也可以通过Notification Center进行通信。

2) Having the locationManager member directly in AppDelegate , and being the delegate itself. 2)将locationManager成员直接放在AppDelegate ,并作为委托本身。

Would it be better to encapsulate de locations listening and management in a different class, or handle it directly in AppDelegate ? 将de locations侦听和管理封装在不同的类中,或者直接在AppDelegate处理它会更好吗? Having into account that I have to be able to listen for locations and perform some tasks both in foreground and in background. 考虑到我必须能够在前台和后台监听位置并执行某些任务。

Thanks in advance 提前致谢

I think your best strategy is to have a singleton instance of a class that is a locationManager delegate. 我认为你最好的策略是拥有一个类的单例实例,它是一个locationManager委托。 This instance would be responsible to filter whatever comes back from locationManager and ultimately assigns the location to one of its properties, let's call it myLoc . 这个实例负责过滤从locationManager返回的任何内容,并最终将位置分配给它的一个属性,让我们称之为myLoc

This solves your centralized logic issues where the location needs post-processing after being acquired. 这解决了您的集中逻辑问题,其中位置需要在获取后进行后处理。

Second, in order to get the correct UIViewControllers and other class instances to do what they need when the location is updated, I suggest you use Key-Value Observing (KVO) on myLoc . 其次,为了在更新位置时获取正确的UIViewControllers和其他类实例以执行所需的操作,我建议您在myLoc上使用键值观察(KVO)。 I assume you know how that works. 我假设你知道它是如何工作的。 The reason I suggest KVO is that you need to trigger changes in multiple areas of the code based on the location being changed, and KVO is made for that kind of pattern. 我建议KVO的原因是你需要根据被改变的位置触发代码的多个区域的变化,并且KVO是针对那种模式而制作的。

I would like to add more to @Rikkles answer by saying that you can create a BaseViewController , which is a subclass of UIViewController and has an instance of your singleton locationManager class too. 我想在@Rikkles的答案中添加更多内容,说你可以创建一个BaseViewController ,它是UIViewController的子类,也有你的单例locationManager类的实例。 This approach allows us to move all the redundant code like, checking reachability, showing activity indicator,getting user location, etc to only one place and will be available to all other classes implementing it. 这种方法允许我们移动所有冗余代码,例如,检查可达性,显示活动指示符,获取用户位置等,只有一个地方,并且可供所有其他实现它的类使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM