如何使用变量初始化新的 Firebase @FirestoreQuery 属性 包装器?

How to initialize the new Firebase @FirestoreQuery property wrapper with variables?

我使用新的@FirestoreQuery 属性 包装器,它使用下面的代码。但是我需要将 it/initialize 与变量一起使用,我该怎么做(见下文我正在尝试做什么)?

import SwiftUI
import FirebaseFirestoreSwift

struct TestFirestoreQuery: View {
    @State var itemLimit: Int = 5
    let increment: Int = 5
    
    @FirestoreQuery(collectionPath: "/discussions/blablabal/messages",
                    predicates: [
                        .order(by: "dateCreated", descending: true),
                        .limit(to: 5)
                    ]
    ) fileprivate var MessageResults: Result<[Message_M], Error>
    
    var body: some View {
        VStack {
            if case let .success(Messages) = MessageResults {
                List(Messages.reversed()) { message in
                    Text("\(message.msg)")
                }
                .refreshable{
                    itemLimit += increment
                    $MessageResults.predicates = [
                        .order(by: "dateCreated", descending: true),
                        .limit(to: itemLimit)
                    ]
                }
            }
            else if case let .failure(error) = MessageResults {
                // Handle error
                Text("Couldn't map data: \(error.localizedDescription)")
            }
            Button {
                itemLimit += increment
                $MessageResults.predicates = [
                    .order(by: "dateCreated", descending: true),
                    .limit(to: itemLimit)
                ]
            } label: {
                Text("Tap me!")
                    .padding()
                    .foregroundColor(.white)
                    .background(.red)
            }
        }
    }
}

我想做的但是没有用:

...
    @State var itemLimit: Int = 5
    let increment: Int = 5
    let path: String = "/discussions/blablabal/messages"
    
    @FirestoreQuery(collectionPath: path,
                    predicates: [
                        .order(by: "dateCreated", descending: true),
                        .limit(to: itemLimit)
                    ]
    ) fileprivate var MessageResults: Result<[Message_M], Error>

...

我遇到了这些错误:

好的,您可以在 onAppear 中初始化它们:

.onAppear {
           $MessageResults.path = path

           $MessageResults.predicates = [
                          .order(by: "dateCreated", descending: true),
                          .limit(to: itemLimit)
                          ]

}

在创建 @FirestoreQuery 时,路径变量尚不可用。

为什么需要路径是一个单独的变量?直接将字符串传递给 @FirestoreQuery 不是更容易吗?

@FirestoreQuery(collectionPath: "/discussions/blablabal/messages",
                predicates: [
                    .order(by: "dateCreated", descending: true),
                    .limit(to: itemLimit)
                ]

这样您就不必添加 onAppear 修饰符。