【问题标题】:Using MapKit causes Publishing changes from within view updates is not allowed, this will cause undefined behavior使用 MapKit 导致不允许从视图更新中发布更改,这将导致未定义的行为
【发布时间】:2022-12-03 20:18:40
【问题描述】:

使用 Swift 5.7、XCode 14.0、iOS 16.0、

尝试使 MapKit 示例工作时,我在 XCode 控制台中收到非常奇怪的错误消息和警告。

这是日志:

2022-11-01 17:26:51.756834+0100 myApp[3999:834036] Metal API Validation Enabled
2022-11-01 17:26:52.139973+0100 myApp[3999:834036] [PipelineLibrary] Mapping the pipeline data cache failed, errno 22
2022-11-01 17:26:52.192482+0100 myApp[3999:834036] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
2022-11-01 17:26:53.884031+0100 myApp[3999:834036] [SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.
2022-11-01 17:26:53.900265+0100 myApp[3999:834036] [SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.

似乎在 SwiftUI 中,Published 变量与 Bindings 的处理方式发生了变化。

我认为,核心问题在 here 中得到了很好的描述。

而且我假设 Apple 还没有在他们自己的 API 中完成向这种新的 SwiftUI4 行为的过渡。

或者有什么办法可以让Publishing changes bla bla 警告消失??

请在下面查看我的完整代码:

//
//  MyView.swift
//  myApp
//

import SwiftUI
import MapKit

struct MyView: View {
    
    @State private var showMap = false
    @State private var region = MKCoordinateRegion(
            center: CLLocationCoordinate2D(
                latitude: 37.8879948,
                longitude: 4.1237047
            ),
            span: MKCoordinateSpan(
                latitudeDelta: 0.05,
                longitudeDelta: 0.05
            )
        )
    @State private var locations: [Location] = [Location(name: "Test", description: "", latitude: 37.8879948, longitude: 4.1237047)]
    @State private var isLoading = false
    
    var body: some View {
        
        Map(coordinateRegion: $region,
            annotationItems: locations,
            annotationContent: { location in
                MapAnnotation(
                    coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
                ) {
                    VStack {
                        Image("THPin")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 44, height: 44)
                        ZStack {
                            Text(location.name)
                                .padding(5)
                                .font(.subheadline)
                                .background(.white.opacity(0.5), in: Capsule())
                        }
                    }
                }
            }
        )
    }
}

【问题讨论】:

  • 如果没有最小的完整示例,则很难确定您尝试进行的更改在何处触发此警告。
  • 这是整个最小的完整示例!只需用手指缩放或捏住地图,您就会看到错误和警告……尝试代码,您会看到……(更新到 XCode14.1、iOS16.1 时也是如此)。

标签: swift swiftui annotations mapkit


【解决方案1】:

同样的问题!我发现如果将 MapAnnotation 替换为 MapMarker,问题就会消失。问题很可能出在图书馆本身

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 2023-02-01
    • 2023-02-06
    • 2020-03-11
    • 2020-05-15
    • 2021-09-17
    • 2022-10-04
    • 2021-01-10
    • 2020-10-27
    相关资源
    最近更新 更多