gpt4 book ai didi

swift - 从 url 异步加载 anchor 不起作用

转载 作者:行者123 更新时间:2023-12-05 05:06:10 26 4
gpt4 key购买 nike

使用 LoadAnchorAsync(contentsOf: URL) 加载 anchor 不起作用。这是我的代码:

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")
}

但这每次都会因错误而崩溃:

线程 27:EXC_BREAKPOINT(代码=1,子代码=0x1051e0dfc)

enter image description here

更新:如果我将整个代码块包装在 DispatchQueue.main.async block 中,它不会崩溃,但 reveiceCompletion 或 recieveValue 都不会被调用

最佳答案

你必须在 member 的某个地方存储 subscriber

// 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")
}

关于swift - 从 url 异步加载 anchor 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60075802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com