【问题标题】:Calling init() in view the best way to pull firebase data?调用 init() 以查看提取 firebase 数据的最佳方式?
【发布时间】:2020-10-31 10:04:22
【问题描述】:

我有一个SearchView()

struct SearchView: View {

@ObservedObject var searchViewModel = SearchViewModel()
init() {
    searchViewModel.LoadAllUsers()
}

var body: some View {
    NavigationView {
        VStack {
            List {
                ForEach(searchViewModel.allUsers, id: \.uid) { user in
                    ......
                }
            }
        }
    }
}

在我的 SearchViewModel 中,我有一个函数可以从 firebase 获取所有用户并将它们存储在 @Published allUsers 数组中:

@Published var allUsers: [User] = []

func LoadAllUsers() {
     Firestore.firestore().collection("users").document(Auth.auth().currentUser!.uid).getDocument{(snapshot, error) in
    ....
}

每次我点击SearchView() 它都会运行函数并调用firebase 并获取数据。这是最好的方法吗?从长远来看,这会增加firebase的成本吗?有没有办法在每次加载应用而不是每次加载SearchView() 时调用此函数?

更新

var window: UIWindow?
var searchViewModel = SearchViewModel()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // Create the SwiftUI view that provides the window contents.
    //let contentView = ContentView()
    
    searchViewModel.LoadAllUsers()
    
    // Use a UIHostingController as window root view controller.
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: InitialView().environmentObject(SessionStore()).environmentObject(searchViewModel))
        self.window = window
        window.makeKeyAndVisible()
    }
}

在我的 SearchViewModel 中,由于以下原因而出现问题:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

由于以下原因:

Auth.auth().currentUser!.uid

【问题讨论】:

    标签: swift firebase swiftui


    【解决方案1】:

    在这种情况下,将其放入SceneDelegate 并作为环境对象注入层次结构是合适的,例如

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
        var window: UIWindow?
        var searchViewModel = SearchViewModel() // create
    
        ...
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
           searchViewModel.LoadAllUsers()        // << preload !!
           ...
           let contentView = ContentView().environmentObject(searchViewModel) // << inject !!
    
    

    struct SearchView: View {
        @EnvironmentObject var searchViewModel: SearchViewModel    // << use !!
        // nothing in init
    
    

    【讨论】:

    • 我进行了以下更改,但由于何时调用它而仍然导致我出现问题?我已经更新了我的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2017-03-29
    • 2018-11-07
    相关资源
    最近更新 更多