SwiftUI-检索数据后导航查看

问题描述

因此,我正在从FireStore检索数据。我正在成功检索数据。当我第一次点击搜索按钮时,将下载数据并推送新视图。结果,我得到一个空白视图。但是当我返回时,再次点击搜索,可以肯定地看到了我的数据。

如何确保我首先拥有要搜索的数据,然后导航到新视图?我使用了@State变量等。但是似乎没有任何效果。我正在使用MVVM方法。

我的ViewModel:

class SearchPostsViewModel: ObservableObject {
    var post: [PostModel] = []
 @State var searchCompleted: Bool = false
    func searchPosts(completed: @escaping() -> Void,onError: @escaping(_ errorMessage: String) -> Void) {
            isLoading = true
        API.Post.searchHousesForSale(propertyStatus: propertyStatus,propertyType: propertyType,location: location,noOfBathrooms: noOfBathroomsValue,noOfBedrooms: noOfBedroomsValue,price: Int(price!)) { (post) in
            self.post = post
            print(self.post.count)
            self.isLoading = false
            self.searchCompleted.toggle()
        }
    

    }
    
}

有效但有错误的代码:

                    NavigationLink(destination: FilterSearchResults(searchViewModel: self.searchPostsViewModel)
                        .onAppear(perform: {
                            DispatchQueue.main.async {
                                self.createUserRequest()
                            }
                        })
                        )
                      {
                        Text("Search").modifier(UploadButtonModifier())
                    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)