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

问题描述 投票:0回答:2

使用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.

在 SwiftUI 中,处理已发布变量与绑定结合的方式似乎发生了变化。

我认为,核心问题已经很好地描述了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())
                        }
                    }
                }
            }
        )
    }
}
swift swiftui annotations mapkit
2个回答
11
投票

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


0
投票

人们一直在向 Apple 反馈有关 MapAnnotation 的反馈,从而导致警告:“不允许从视图更新中发布更改;这将导致未定义的行为。”看来苹果已经承认了这个问题并正在积极致力于修复。

根据一个月前相关论坛帖子的最新评论,这个问题似乎将在 Xcode 15.0 和 iOS 17.0 中得到解决。

与此同时,除了即将推出的解决方案之外,没有解决这些警告的指南。现在看来可以安全地忽略它们,因为这是苹果需要纠正的错误。

更多详情,您可以参考Apple开发者论坛上的this帖子。

© www.soinside.com 2019 - 2024. All rights reserved.