0

我试图在每 X 分钟后将位置从应用程序更新到 firebase(任何小于 15 分钟的值都很好)。

我已经为位置启用了后台模式,并通过以下方法尝试上传到 Firestore 文档。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

问题在日志中我可以看到这个方法被调用但在后台没有数据上传到firestore。事实上,我有时会在应用程序进入后台后工作,但在那之后就完全没有了。

有什么解决方法吗?

4

3 回答 3

1

使用此代码

locationManager.startUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.delegate = self

不要在以下委托方法中停止位置更新。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
于 2021-06-02T10:40:22.710 回答
1

检查您的位置管理器中的allowBackgroundLocationUpdates 是否设置为true

locationManager?.allowsBackgroundLocationUpdates = true

只有在位置有任何更新时,您才会在后台更新位置。还要检查您是否在位置管理器中提到了任何距离过滤器。如果是这样,基于此,您将每 X 米获得一次位置更新

locationManager?.distanceFilter = CLLocationDistance(X)

于 2021-06-02T09:57:43.573 回答
1

使用此代码和其中的 Timer 在 Firestore 中调用更新:

在开始地图调用时编写此代码:

locationManager.startUpdatingLocation()
    locationManager.delegate = self

在您不想停止位置更新之前避免调用:

locationManager.stopUpdatingLocation()

符合委托方法:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = manager.location else {
        return
    }
    
    self.clLocation = location
    //Firestore condition here

}
于 2021-06-02T09:59:12.450 回答