I sort of fixed the issue by modifying addNode as follows:
func addNode(annotationLocation: ARCL.LocationAnnotationNode) {
if let sceneNode=sceneLocationView.sceneNode {
sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: annotationLocation, sceneNode: sceneNode)
} else {
let completion: ((SCNNode)->Void)={(sceneNode) in
DispatchQueue.main.async {
print("sceneNode" + (sceneNode.description))
self.sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: annotationLocation, sceneNode:sceneNode)
sceneLocationView.focusItems(in: annotationLocation.frame)
}
}
let nodeComposition = NamedSceneNode(sceneNodeCompletion: completion)
nodeComposition.progress()
print("count \(nodeComposition.counter)")
if var viewController=UIApplication.topViewController() as? ViewControllerProtocol{
if !viewController.nodeShowingCompletions.contains(nodeComposition){
viewController.nodeShowingCompletions.insert(
nodeComposition
)
}
}
}
With:
struct NamedSceneNode: Hashable{
static func == (lhs: NamedSceneNode, rhs: NamedSceneNode) -> Bool {
lhs.counter == rhs.counter
}
var sceneNodeCompletion: ((SCNNode)->Void)
static var count: Int=0
var counter: Int{
return NamedSceneNode.count
}
func progress(){
NamedSceneNode.count+=1
print("counter ora: \(counter)")
}
func hash(into hasher: inout Hasher) {
hasher.combine(counter)
}
}
And then executing the completions in:
func sceneLocationViewDidUpdateLocationAndScaleOfLocationNode(sceneLocationView: SceneLocationView, locationNode: LocationNode) {
guard let sceneNode=sceneLocationView.sceneNode else {
return
}
print("updated element"+locationNode.description + "sceneNode: " + (sceneLocationView.sceneNode?.description ?? "nil"))
for nodeCompletion in self.nodeShowingCompletions {
nodeCompletion.sceneNodeCompletion(sceneNode)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.nodeShowingCompletions.remove(nodeCompletion)
}
}
}
Of course recycling the same sceneNodes over and over again.