Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading anchor from url async doesnt work

Loading a anchor using LoadAnchorAsync(contentsOf: URL) does not work. Here is my code:

if let url = url {
    let loadRequest = Entity.loadAnchorAsync(contentsOf: url)
    _ = loadRequest.sink(receiveCompletion: { completion in
        // handle completion
    }, receiveValue: { anchor in
        self.arView.scene.addAnchor(anchor)
    })
} else {
        fatalError("no url")
}

but this crashes every time with an error:

Thread 27: EXC_BREAKPOINT (code=1, subcode=0x1051e0dfc)

enter image description here

Update: If i wrap the entire code block in an DispatchQueue.main.async block it doenst crash but neither reveiceCompletion or recieveValue gets called

like image 720
Magnus T Avatar asked Nov 25 '25 03:11

Magnus T


1 Answers

You have to store subscriber somewhere in member

// somewhere above
private var subscribers: Set<AnyCancellable>()

...
if let url = url {
    let loadRequest = Entity.loadAnchorAsync(contentsOf: url)
    loadRequest.sink(receiveCompletion: { completion in
        // handle completion
    }, receiveValue: { anchor in
        self.arView.scene.addAnchor(anchor)
    }).store(in: &self.subscribers) // << keep it alive
} else {
        fatalError("no url")
}
like image 61
Asperi Avatar answered Nov 27 '25 22:11

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!