使用 MapKit 导致不允许从视图更新中发布更改,这将导致未定义的行为

iKK*_*iKK 14 annotations mapkit swift swiftui

使用Swift5.7、XCode14.0、iOS16.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.
Run Code Online (Sandbox Code Playgroud)

看来在 SwiftUI 中,发布的变量与绑定的处理方式发生了变化。

我认为,这里很好地描述了核心问题。

我认为 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())
                        }
                    }
                }
            }
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 11

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

  • 是的!我一直在努力思考这是我的代码的问题...但是,这似乎是与 MapAnnotation 相关的错误。切换回 MapMarker 会使警告消失。问题是我无法拥有自定义标记 (3认同)