【发布时间】:2020-04-22 19:07:00
【问题描述】:
我正在尝试使用 swiftUI 在后台线程中运行此搜索,但我不知道在哪里以及如何放置代码 DispatchQueue.global().async {}
我试图在主视图中渲染显示结果的列表,但给了我错误。我尝试将它放在运行搜索的函数中,但也会给我错误。
这里是我对搜索栏和功能的看法
var body: some View {
VStack {
// fakebar
SearchBar(text: $searchTerm)
List{
ForEach(dm.filter(valoreSearhed: searchTerm, arrayTosearh: dm.airportVector)) { item in
Text(item.aptICAO)
}
}
}
}
这里是我的搜索功能
func filter (valoreSearhed: String, arrayTosearh: AirportVector) -> [AirportModel] {
// if I use like this Xcode give me warning Cannot convert return expression of type '()' // to return type '[AirportModel]'
DispatchQueue.global().async {
arrayTosearh.filter {
// valoreSearhed.isEmpty ? true :
$0.aptICAO.localizedCaseInsensitiveContains(valoreSearhed)
}
}
}
如果我删除调度队列,搜索会完美运行,但它会在几秒钟内挡住我的视线。
谢谢
【问题讨论】:
标签: swift xcode multithreading list swiftui