gpt4 book ai didi

firebase - SwiftUI:从 DetailView 更新 Firestore

转载 作者:行者123 更新时间:2023-12-05 06:51:26 25 4
gpt4 key购买 nike

我正在努力将 Firestore 存储库整合到 SwiftUI 2 应用程序中。 ListView 会适当加载,并在 Firestore 集合中的数据更改时自动刷新。 NavigationLink 正确加载适当的 DetailView。 DetailView 上的一个按钮设置为更改相关文档中的数据,并且工作正常,但是 DetailView 页面没有刷新正确的数据(退出到 ListView 并返回时正确显示);同样,DetailView 不会响应在其他地方对集合所做的更改。

我尝试使用 ObservedObject 和 State 的各种配置,但一直无法获得预期的结果。

如有任何帮助,我们将不胜感激。下面列出的代码片段显示了流程,并以状态字段为例。

客户资料库

class CustomerRepository: ObservableObject {

private let path: String = "customers"
private let store = Firestore.firestore()
@Published var customers: [Customer] = []

private var cancellables: Set<AnyCancellable> = []

init() {
store.collection(path)
.addSnapshotListener { querySnapshot, error in
if let error = error {
print("Error getting customers \(error.localizedDescription)")
return
}

self.customers = querySnapshot?.documents.compactMap { document in

try? document.data(as: Customer.self)

} ?? []
}
}

func update(_ customer: Customer) {
guard let customerID = customer.id else { return }

do {
try store.collection(path).document(customerID).setData(from: customer)
} catch {
fatalError("Unable to update record: \(error.localizedDescription)")
}
}
}

CustomerListViewModel

class CustomerListViewModel: ObservableObject {

@Published var customerViewModels: [CustomerViewModel] = []

private var cancellables: Set<AnyCancellable> = []

@Published var customerRepository = CustomerRepository()

init() {
customerRepository.$customers.map { customers in
customers.map(CustomerViewModel.init)
}
.assign(to: \.customerViewModels, on: self)

.store(in: &cancellables)
}
}

CustomerViewModel

class CustomerViewModel: ObservableObject, Identifiable {

private let customerRepository = CustomerRepository()

@Published var customer: Customer

private var cancellables: Set<AnyCancellable> = []

var id = ""

init(customer: Customer) {
self.customer = customer

$customer
.compactMap { $0.id }
.assign(to: \.id, on: self)
.store(in: &cancellables)
}

func update(customer: Customer) {
customerRepository.update(customer)
}
}

客户 ListView

struct CustomerListView: View {

@ObservedObject var customerListViewModel = CustomerListViewModel()

var body: some View {
NavigationView {
List {
ForEach(customerListViewModel.customerViewModels) { customerViewModel in

NavigationLink(
destination: CustomerDetailView(customerViewModel: customerViewModel)) {
CustomerCell(customerViewModel: customerViewModel)
}
}
}
}
}
}

CustomerDetailView

struct CustomerDetailView: View {
var customerViewModel: CustomerViewModel

var body: some View {
VStack {

Text(customerViewModel.customer.status.description)

}.onTapGesture {
nextTask()
}
}

private func nextTask() {
switch customerViewModel.customer.status {
case .dispatched:
customerViewModel.customer.status = .accepted

...

default:
return
}

update(customer: customerViewModel.customer)

}

func update(customer: Customer) {
customerViewModel.update(customer: customer)
}
}

最佳答案

经过一些重新配置,以及这个例子的帮助:

https://peterfriese.dev/swiftui-firebase-update-data/

我能够解决我的问题。以下是修改后代码的分割,以防对其他人有所帮助....

CustomerViewModel

class CustomerViewModel: ObservableObject, Identifiable {

@Published var customer: Customer
@Published var modified = false

private var cancellables: Set<AnyCancellable> = []

init(customer: Customer = Customer(status: .new)) {
self.customer = customer

self.$customer
.dropFirst()
.sing { [weak self] customer in
self?.modified = true
}
.store(in: &cancellables)
}

func handleDoneTapped() {
self.updateOrAddCustomer()
}
}

客户 ListView

// Snip to Navigation Link

NavigationLink(
destination: CustomerDetailView(customerViewModel: CustomerViewModel(customer: customerViewModel.customer))) {
CustomerCell(customerViewModel: customerViewModel)
}

CustomerDetailView

struct CustomerDetailView: View {
@ObservedObject var customerViewModel = CustomerViewModel()

var body: some View {
VStack {

Text(customerViewModel.customer.status.description)

}.onTapGesture {
nextTask()
}
}

private func nextTask() {
switch customerViewModel.customer.status {
case .dispatched:
customerViewModel.customer.status = .accepted

...

default:
return
}

update(customer: customerViewModel.customer)

}

func update(customer: Customer) {
customerViewModel.handleDoneTapped()
}
}

关于firebase - SwiftUI:从 DetailView 更新 Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66160285/

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