efremidze / Cluster

Easy Map Annotation Clustering 📍
MIT License
1.27k stars 121 forks source link

Background thread issue leading to a crash #129

Open letko-dmitry opened 4 years ago

letko-dmitry commented 4 years ago

https://github.com/efremidze/Cluster/blob/3c70568c779456355895adf354a01e7fe5440ebf/Sources/Cluster.swift#L335

The line of code above triggers UI update via KVO on background thread and it leads to a crash because of precondition.

NSInternalInconsistencyException Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.
0  CoreFoundation           __exceptionPreprocess + 220
1  libobjc.A.dylib          objc_exception_throw + 56
2  Foundation               -[NSISEngine tryToOptimizeReturningMutuallyExclusiveConstraints] + 0
3  Foundation               -[NSISEngine withBehaviors:performModifications:] + 32
4  UIKitCore                -[UIView(Hierarchy) _postMovedFromSuperview:] + 776
5  UIKitCore                -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1600
6  MapKit                   -[MKAnnotationContainerView addAnnotationView:allowAnimation:] + 1444
7  MapKit                   -[MKMapView addAnnotationRepresentation:allowAnimation:] + 588
8  MapKit                   -[MKAnnotationManager _addRepresentationForAnnotation:] + 668
9  MapKit                   -[MKAnnotationManager updateVisibleAnnotations] + 1008
10 MapKit                   -[MKAnnotationManager observeValueForKeyPath:ofObject:change:context:] + 996
11 Foundation               NSKeyValueNotifyObserver + 288
12 Foundation               NSKeyValueDidChange.llvm.18207776532572868477 + 332
13 Foundation               NSKeyValueDidChangeWithPerThreadPendingNotifications.llvm.18207776532572868477 + 148
14 Foundation               NSKVOForwardInvocation + 336
15 CoreFoundation           ___forwarding___ + 676
16 CoreFoundation           _CF_forwarding_prep_0 + 92
17 My App                   closure #3 in ClusterManager.distributeAnnotations(tree:mapRect:) + 824 (Cluster.swift)
efremidze commented 4 years ago

I don't see any preconditions in the framework that could cause this. This is probably a MapKit exception.

letko-dmitry commented 4 years ago

Em, yes, exactly. And it forces us to set coordinate of an annotation on the main thread.

crgt5252 commented 2 years ago

I'm seeing this crash in our app as well. Any workarounds?

webcpu commented 1 year ago
DispatchQueue.main.async {
   self.clusterManager.reload(self.mapView)
}