gpt4 book ai didi

swift - 如何修复由于未捕获的异常 'RLMException' 、原因 : 'Realm accessed from incorrect thread.' ? 而终止应用程序

转载 作者:行者123 更新时间:2023-11-30 11:48:10 25 4
gpt4 key购买 nike

如何解决此问题由于未捕获的异常“RLMException”而终止应用程序,原因:“从不正确的线程访问 Realm 。”,我花了 2 天寻找此问题的解决方案。我希望有人能帮助我。我尝试调试和跟踪它,发现它成功进入 .subscribe(onNext:) 并读取 controller?.tableView.reloadData() 但如果我继续程序执行它只会终止并在控制台日志中显示错误。

        searchBar
.rx.text
.orEmpty
.debounce(0.5, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.filter { !$0.isEmpty }
.map({ [weak controller = self] query -> (tcb: [TrialCourtBranches], query: String)? in
switch controller?.tcbArray {
case .some(let array):
return (array, query)
case .none:
return nil
}
})
.observeOn(ConcurrentDispatchQueueScheduler(qos: .userInitiated)) // change thread before filtering
.map({ (query) -> [TrialCourtBranches] in // filter and return the result of filtering
let realm = try! Realm()
let tcb = realm.objects(TrialCourtBranches.self)
let tcb_safe = ThreadSafeReference(to: tcb)
let filtered = realm.resolve(tcb_safe)
let tcb_filtered:[TrialCourtBranches] = (filtered?.filter({ $0.branch_name.lowercased().contains(searchText.lowercased()) }))!
return tcb_filtered
})
.observeOn(MainScheduler.instance) // go back to main thread
.subscribe(onNext: { [weak controller = self] (filtered) in
tcb_filteredArray = filtered
controller?.tableView.reloadData()
})
.disposed(by: bag)

最佳答案

请注意,我并不是真正的 IOS 开发人员,因此这段代码很容易在语法上出现错误,但问题是这并不是在 Realm 中进行过滤的好方法。

例如,您可以使用 https://github.com/RxSwiftCommunity/RxRealm 中的 RxRealm它允许您将结果公开为可观察的变更集。

然后您可以在其上切换Map(或用您的语言,flatMapLatest)。

所以最终的结果会是这样的

extension UITableView {
func applyChangeset(_ changes: RealmChangeset) {
beginUpdates()
deleteRows(at: changes.deleted.map { IndexPath(row: $0, section: 0) }, with: .automatic)
insertRows(at: changes.inserted.map { IndexPath(row: $0, section: 0) }, with: .automatic)
reloadRows(at: changes.updated.map { IndexPath(row: $0, section: 0) }, with: .automatic)
endUpdates()
}
}

还有

    searchBar
.rx.text
.orEmpty
.debounce(0.5, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.filter { !$0.isEmpty }
.map({ [weak controller = self] query -> (tcb: [TrialCourtBranches], query: String)? in
switch controller?.tcbArray {
case .some(let array):
return (array, query)
case .none:
return nil
}
})
.flatMapLatest({ (query) -> [TrialCourtBranches] in // filter and return the result of filtering
let realm = try! Realm()
let results = realm.objects(TrialCourtBranches.self)
.filtered($0.branch_name.lowercased()
.contains(searchText.lowercased()))
return Observable.changeset(from: results)
})
.subscribe(onNext: {[unowned self] results, changes in
if let changes = changes {
controller?.tableView.applyChangeset(changes)
} else {
controller?.tableView.reloadData()
}
})
.disposed(by: bag)

关于swift - 如何修复由于未捕获的异常 'RLMException' 、原因 : 'Realm accessed from incorrect thread.' ? 而终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594514/

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