简体   繁体   中英

UI not updating when called on main thread Swift

I am having a hard time getting my annotation's subtitle to update, I am calling it on the main thread and ensuring that the result is correct.

Can anyone see why it would not be updating?

 let stopAnnotation: UUStopAnnotation = view.annotation as! UUStopAnnotation

        // Else get the stop estimation
        webService?.getStopEstimation(routeId: stopAnnotation.routeId, stopId: stopAnnotation.stopId, completion: { (result) in

            print(result)
            DispatchQueue.main.async(execute: {
                stopAnnotation.subtitle = result
            })

        })

You can't change an annotation in the map. Remove this annotation and replace it with one that has the same coordinates but the new subtitle.

您是否尝试过强制地图视图重绘?

view.setNeedsDisplay()

I fixed it by adding this:

    webService?.getStopEstimation(routeId: stopAnnotation.routeId, stopId: stopAnnotation.stopId, completion: { (result) in

        print(result)
        DispatchQueue.main.async(execute: {
            mapView.removeAnnotation(view.annotation!)
            stopAnnotation.subtitle = result
            mapView.addAnnotation(stopAnnotation)
            mapView.selectAnnotation(stopAnnotation, animated: true)
        })

    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM