gpt4 book ai didi

ios - SwiftUI 可观察对象不更新 UI

转载 作者:行者123 更新时间:2023-11-28 13:23:22 30 4
gpt4 key购买 nike

我有一个可观察对象,它从 View 上的 onAppear 操作加载银行列表。进行更改后,我退出 View ,然后返回调用 loadBankMembers 的 View ,但这些更新未反射(reflect)在 UI 中。如果我退出并返回,则会反射(reflect)更新。

import SwiftUI

struct BankMembersView: View {
@EnvironmentObject var userData: UserData

var body: some View {
ZStack {
if userData.showPlaidModal {
PlaidView()
}

List {
ForEach(userData.bankMembers) { bankMember in
BankMemberRowView(bankMember: bankMember)
}
}
}
.onAppear(perform: { self.userData.loadBankMembers() })
.navigationBarTitle("Bank Accounts")
.navigationBarItems(trailing:
Button(action: { self.userData.showPlaidModal = true }) {
Text("Add")
}
)
}
}
import SwiftUI
import Combine

final class UserData: ObservableObject {
let objectWillChange = ObservableObjectPublisher()

let apollo = Apollo()

var bankMembers: [BankMember] = [] {
willSet { self.objectWillChange.send() }
}

func loadBankMembers() {
apollo.client.fetch(query: ListBankMembersQuery(), cachePolicy: .fetchIgnoringCacheCompletely) { result in
guard let data = try? result.get().data else { return }
var bankMembers: [BankMember] = []

for bankMemberData in data.bankMembers ?? [] {
var bankAccounts: [BankAccount] = []

for bankAccountData in bankMemberData?.bankAccounts ?? [] {
if let bankAccountid = bankAccountData?.id {
let bankAccount = BankAccount(id: bankAccountid, name: bankAccountData?.name ?? "", sync: bankAccountData?.sync ?? false)
bankAccounts.append(bankAccount)
}
}

if let id = bankMemberData?.id {
let bankMember = BankMember(id: id, name: bankMemberData?.name ?? "", status: bankMemberData?.status, bankAccounts: bankAccounts)
bankMembers.append(bankMember)
}
}

self.bankMembers = bankMembers
}
}
}

我尝试在加载开始时设置 UserData.banksMembers = [] 但随后出现崩溃并出现此错误:

2019-11-07 20:51:58.191689-0700 Spendable[5839:255244] [TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView: 0x7fe05689fa00; baseClass = UITableView; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000813990>; layer = <CALayer: 0x60000064be20>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <_TtGC7SwiftUIP13$7fff2c6b223419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___: 0x7fe0564235c0>>
2019-11-07 20:51:58.191948-0700 Spendable[5839:255244] *** Assertion failure in -[_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3900.12.15/UITableView.m:2039
2019-11-07 20:51:58.198956-0700 Spendable[5839:255244] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete section 0, but there are only 0 sections before the update'

最佳答案

您错过了 var bankMembers 的 @Published。让我展示一个简单的例子(不能使用你的代码,所以写了类似的东西):

final class UserData: ObservableObject  {

@Published var bankMembers: [String] = []

func loadBankMembers() {
bankMembers.append("Steve")
bankMembers.append("Bob")
}

func updateBankMembers() {
bankMembers.append("John")
bankMembers.append("Ann")
}
}
struct BankMembersView: View {
@EnvironmentObject var userData: UserData

var body: some View {

NavigationView {
List {
ForEach(userData.bankMembers, id: \.self) { bankMember in
Text(bankMember)
}
}
.onAppear(perform: { self.userData.loadBankMembers() })
.navigationBarTitle("Bank Accounts")
.navigationBarItems(trailing:
Button(action: { self.userData.updateBankMembers() }) {
Text("Add")
}
)
}
}
}

一开始你会得到: enter image description here

按下“添加”按钮后: enter image description here

P.S.找好article针对您的情况了解更多信息

关于ios - SwiftUI 可观察对象不更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760121/

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