gpt4 book ai didi

ios - 当模型在 @Published 属性内更新时,SwiftUI 触发函数

转载 作者:行者123 更新时间:2023-12-03 08:14:04 24 4
gpt4 key购买 nike

我从 Firestore 表中获取一个数组。显然是实时的,因此任何新添加的文档或对现有文档的修改都会反射(reflect)在 View 中。这适用于字符串和数字等简单属性,但我也需要显示和刷新其他数据。

我还存储坐标,如果坐标发生变化,我需要调用 geocoder.reverseGeocodeLocation() 以便从该位置获取地标并将地址显示为字符串。为此,我创建了一个名为 PlacemarkService 的服务(代码如下)。

问题是这样的。每当模型被修改时,数据都会通过 PackagesViewModel 来,它有一个 @Published var results = [Package]() 这是由 Firestore 动态修改的(可编码)。然后,对数组造成的任何更改都将导致 PackagesView 内的 List 被刷新,这将导致 PackageView(packageService: PackageService(package: package)) 被创建,并且由于 PackageService 作为参数传递,因此将创建 PackageService 的新实例。最后这将触发该服务内的 init() 被调用。

@Published var results = [Package]() -> List -> PackageView(packageService: PackageService(package: package)) -> init()

现在,如果我遵循此流程并在 init 内触发 getPlacemarks() ,一切都会正常工作(大多数情况下),并且如果包模型发生更改,该函数将在 init 上调用,并将反转 GeocodeLocation修改后的位置。但这里有一个大问题,这就是原因。我无法将此逻辑放在 init 上,因为我无法对 for 循环内的数百个位置进行地理编码。我在列表中显示了多个包,而 MapKit 不允许这样做。

很明显,这个逻辑需要在显示 PackageView 之后触发,因此当选择这些包之一时。

如果我在 PackageView 出现时调用 packageService.getPlacemarks() ,那么第一次工作正常,但是...当 @Published var results = [Package]( 时,不会触发 onAppear ) 已更新。

最后的问题是:

在哪里调用 packageService.getPlacemarks() ,以便每当 @Published 内的 Package 时都会调用它 var results = [Package] 已更新

但由于我上面解释的原因,不在 PlacemarkService 的初始化中。

抱歉这么长的解释,我只是想弄清楚。

class PackageService: ObservableObject {
var package: Package
private var cancellables = Set<AnyCancellable>()

@Published var sourcePlacemarkService = PlacemarkService()
@Published var destinationPlacemarkService = PlacemarkService()

var cancellable: AnyCancellable? = nil

init(package: Package) {
self.package = package
startListening()
}

func startListening() {
// Listen to placemark changes.
cancellable = Publishers.CombineLatest(sourcePlacemarkService.$placemark, destinationPlacemarkService.$placemark).sink(receiveValue: {_ in

// Publish changes manually to the view.
self.objectWillChange.send()
})
}

// Get placemarks from locations
func getPlacemarks() {
sourcePlacemarkService.reverseGeocodeLocation(location: package.source.toCLLocation)
destinationPlacemarkService.reverseGeocodeLocation(location: package.destination.toCLLocation)
}
}

class PackagesViewModel: ObservableObject {
@Published var results = [Package]()

func load() {
query.addSnapshotListener { (querySnapshot, error) in
// updates results array when a document is modified.
}
}
}


struct PackagesView: View {
@StateObject var packagessViewModel = PackagesViewModel()

var body: some View {
List(packagessViewModel.results, id: \.self) { package in
NavigationLink(destination: PackageView(packageService: PackageService(package: package))) {
Text(package.title)
}
}
}
}

struct PackageView: View {
@ObservedObject var packageService: PackageService

func onAppear() {
packageService.getPlacemarks()
}

var body: some View {
// show the address from placemark after it is geocoded.
VStack {
Text(packageService.sourcePlacemarkService.placemark.title)
Text(packageService.destinationPlacemarkService.placemark.title)
}
.onAppear(perform: onAppear)
}
}

class PlacemarkService: ObservableObject {
@Published var placemark: CLPlacemark?

init(placemark: CLPlacemark? = nil) {
self.placemark = placemark
}

func reverseGeocodeLocation(location: CLLocation?) {
if let location = location {
geocoder.reverseGeocodeLocation(location, completionHandler: { (placemark, error) in
// some code here
self.placemark = placemark
})
}
}
}

struct Package: Identifiable, Codable {
@DocumentID var id: String?
var documentReference: DocumentReference

var uid: String
var title: String
var description: String
var source, destination: GeoPoint
var amount: Double

var createdAt: Timestamp = Timestamp()
var paid: Bool = false
}

最佳答案

首先 - 简化这段代码。大部分代码对于重现问题来说是不必要的,甚至无法编译。下面的代码不是工作代码,而是我们稍后必须开始和更改的代码:

内容 View

struct ContentView: View {
var body: some View {
NavigationView {
PackagesView()
}
}
}

套餐服务

class PackageService: ObservableObject {
let package: Package

init(package: Package) {
self.package = package
}

// Get placemarks from locations
func getPlacemarks() {
print("getPlacements called")
}
}

PackagesViewModel

class PackagesViewModel: ObservableObject {
@Published var results = [Package]()
}

包 View

struct PackagesView: View {
@StateObject var packagesViewModel = PackagesViewModel()

var body: some View {
VStack {
Button("Add new package") {
let number = packagesViewModel.results.count + 1
let new = Package(title: "title \(number)", description: "description \(number)")
packagesViewModel.results.append(new)
}

Button("Change random title") {
guard let randomIndex = packagesViewModel.results.indices.randomElement() else {
return
}

packagesViewModel.results[randomIndex].title = "new title (\(Int.random(in: 1 ... 100)))"
}

List(packagesViewModel.results, id: \.self) { package in
NavigationLink(destination: PackageView(packageService: PackageService(package: package))) {
Text(package.title)
}
}
}
}
}

包 View

struct PackageView: View {
@ObservedObject var packageService: PackageService

var body: some View {
VStack {
Text("Title: \(packageService.package.title)")

Text("Description: \(packageService.package.description)")
}
}
}

套餐

struct Package: Identifiable, Hashable {
let id = UUID()
var title: String
let description: String
}

现在,解决问题。我通过检测 结果onChange(of:perform:) 的变化解决了这个问题。但是,从这里无法访问 View 主体中使用的 PackageService

为了防止出现此问题,PackageService 实际上存储在 PackagesViewModel 中,这在逻辑上对数据流更有意义。现在,由于 PackageService 也是一个 struct,因此 @Published 在数组上工作以获取 结果,现在可以使用了。

请参阅下面的代码:

PackageService(已更新)

struct PackageService: Hashable {
var package: Package

init(package: Package) {
self.package = package
}

// Get placemarks from locations
mutating func getPlacemarks() {
print("getPlacements called")

// This function is mutating, feel free to set any properties in here
}
}

PackagesViewModel(已更新)

class PackagesViewModel: ObservableObject {
@Published var results = [PackageService]()
}

PackagesView(已更新)

struct PackagesView: View {
@StateObject var packagesViewModel = PackagesViewModel()

var body: some View {
VStack {
Button("Add new package") {
let number = packagesViewModel.results.count + 1
let new = Package(title: "title \(number)", description: "description \(number)")
packagesViewModel.results.append(PackageService(package: new))
}

Button("Change random title") {
guard let randomIndex = packagesViewModel.results.indices.randomElement() else {
return
}

let newTitle = "new title (\(Int.random(in: 1 ... 100)))"
packagesViewModel.results[randomIndex].package.title = newTitle
}

List($packagesViewModel.results, id: \.self) { $packageService in
NavigationLink(destination: PackageView(packageService: $packageService)) {
Text(packageService.package.title)
}
}
}
.onChange(of: packagesViewModel.results) { _ in
for packageService in $packagesViewModel.results {
packageService.wrappedValue.getPlacemarks()
}
}
}
}

PackageView(已更新)

struct PackageView: View {
@Binding var packageService: PackageService

var body: some View {
VStack {
Text("Title: \(packageService.package.title)")

Text("Description: \(packageService.package.description)")
}
}
}

关于ios - 当模型在 @Published 属性内更新时,SwiftUI 触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69982541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com