繁体   English   中英

具有核心位置和核心数据的 SwiftUI 应用程序

[英]SwiftUI App with Core Location and Core Data

我正在开发一个应用程序来跟踪用户的位置并在 CoreData 中保存一些与该位置相关的数据。 我是 SwiftUI 的新手,所以我的问题与应用程序中将位置数据保存到 CoreData 的最佳位置/位置有关。

目前,我的设置与此处接受的解决方案非常相似: How to get Current Location using SwiftUI, without ViewControllers?

因此,我不会重复上面链接中已经存在的代码,但会引用它。 所以你可能想在阅读我的问题陈述的rest之前先看看答案。

所以在 class LocationManager 我有:

@Published var userLocations: [CLLocation]? {
    willSet {
        objectWillChange.send()
    }
}


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }
    let howOldIsTheLastLocationData = abs(location.timestamp.timeIntervalSinceNow)
    if howOldIsTheLastLocationData < 15 && location.horizontalAccuracy > 0 && location.horizontalAccuracy < 15 {
        if userLocations == nil {
            userLocations = []
        }
        userLocations?.append(location)
    }
    print(#function, location, location.latitudeString, location.longitudeString, location.altitude, howOldIsTheLastLocationData)
}

因此 userLocations 是我想在我的视图中观察并保存到我的 CoreData 中的内容。

现在在我看来,我有:

@Environment(\.managedObjectContext) var context
@State var segment: Segment?
@ObservedObject var locationManager = LocationManager()

var userPath: [CLLocationCoordinate2D]? {
    if let userLocations = locationManager.userLocations {            
        return userLocations.map( { $0.coordinate })
    }
    return nil
}

var body: some View {
    // A MapView Uses userPath to show the path on a MKMapView        
    MapView(pathCoordinates: userPath ?? [])
    
    // There is a button that by pressing it, it creates a Segment which is an Entity in my CoreData with time data.
}

到目前为止,我上面提到的一切都在工作。 我正在获取位置更新,在 MapView 上显示路径。 我可以通过按开始按钮来创建分段(并保存到 CoreData)。 现在我正在寻找的部分是针对进入 userLocations 的每个位置更新,我想创建另一个名为 PointOnPath 的 CoreData 实体的新实例,它与一个段关联。 所以 Segment 与 PointOnPath 是一对多的关系。 我不知道我应该在哪里调用类似以下代码行的内容:

  let point = PointOnPath(context: context)
  point.latitude = location.coordinate.latitude
  point.longitude = location.coordinate.longitude
  point.segment = segment
  try? context.save()

我想过将上面的代码行放入:

var userPath: [CLLocationCoordinate2D]? {
    if let userLocations = locationManager.userLocations {  
        // Maybe add to CoreData for PointOnPath here?
        return userLocations.map( { $0.coordinate })
    }
    return nil
}

但我注意到这被多次调用。 在我看来,它应该只在发生新的 userLocations?.append(location) 时才被调用,但事实并非如此,它被调用的次数比 userLocations?.append(location) 发生的次数多得多。 一般来说,我不确定这是否是在 PointOnPath 中保存位置数据的最佳位置。 任何见解都非常感谢。

仍然绝对没有理由让视图负责将实体保存到数据库中。 (甚至访问)既不使用 Storyboard 也不使用 SwiftUI。

根据您的架构,您可以创建一个 ViewModel,它可以访问您的 LocationManager 和一个,比如说,DataManger。 这个负责将内容保存到您的数据库中。

你可能想看看这篇文章。

暂无
暂无

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

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