添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Using MapKit causes Publishing changes from within view updates is not allowed, this will cause undefined behavior

Ask Question

Using Swift5.7, XCode14.0, iOS16.0,

I get very strange error messages and warnings in my XCode console, when trying to make a MapKit example to work.

Here is the log:

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.

It seems that in SwiftUI, there has been a change in how Published variables in combination with Bindings are handeled.

The core issue, I think, is very nicely described here.

And I assume that Apple has not finished the transition to this new SwiftUI4 behaviour in their own API's themselves.

Or is there any way I can make the Publishing changes bla bla warning going away ??

See my entire Code here below:

// 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()) Without a minimal complete example it's hard to work out where the changes you are trying to make trigger this warning. – Magnas Nov 2, 2022 at 8:41 This IS the entire minimal complete example!! Just zoom or pinch the map with your fingers and you see the errors and warnings.... Try the code and you will see... (also the same when updating to XCode14.1, iOS16.1). – iKK Nov 2, 2022 at 10:00 It isn't an MRE because it is missing the Location struct but I think this is a bug, worthy to report to apple. I don't see where the change is being made. – lorem ipsum Dec 3, 2022 at 13:58 Yes! I was struggling with this thinking that it was an issue with my code... However, it mostly seems that is a bug related to MapAnnotation. Switching back to MapMarker makes the warning disappear. The problem is that I won't be able to have custom markers – Jose Flavio Quispe Irrazábal Feb 14 at 22:00

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.