[英]SwiftUI ObservedObject not updating in View
class Room: ObservableObject { ... }
Contact: ObservableObject {
var chatRoom: Room
}
class Account: ObservableObject {
var rooms: Room { … }
var contacts: [Contact] {
return rooms.map {
Contact(chatRoom: $0)
}
}
func listenForRoomEvents() {
// Called on instantiation of a Room, this fires self.objectWillChange on room updates and is working properly
}
}
struct RoomView: View {
@ObservedObject var room: Room
}
/ THIS IS WORKING / / 这是有效的 /
struct ParentView: View {
@EnvironmentObject account: Account
var body: some View {
RoomsView(account.rooms)
.onAppear {
self.account.listenForRoomEvents()
}
}
}
struct RoomsView: View {
var rooms: [Room]
var body: some View {
ForEach(rooms) { room in
NavigationLink(destination: RoomView(room: room)) {
RoomListItemView(room: room)
}
}
}
}
/ THIS IS NOT WORKING / / 这不起作用 /
struct ParentView: View {
@EnvironmentObject account: Account
var body: some View {
Child1(contacts: account.contacts)
.onAppear {
self.account.listenForRoomEvents()
}
}
}
struct Child1: View {
@State var selectedContact: Contact?
var contacts: [Contact]
var body: some View {
RoomView(selectedContact.chatRoom)
UserSelectorView(contacts: contacts, selectedUser: $selectedContact) // View allowing selection of a user
}
}
I outlined my setup above;我在上面概述了我的设置; basically, I am instantiating a RoomView object with a Room instance containing all the chat events and other details.基本上,我正在使用包含所有聊天事件和其他详细信息的 Room 实例实例化 RoomView 对象。 Child1 holds a selected contact state variable which is bound to two of its own subviews, one of which allows for the user to select a different contact and such. Child1 持有一个选定的联系人状态变量,该变量绑定到它自己的两个子视图,其中一个允许用户选择不同的联系人等。
What does not make sense to me is that the RoomView renders just fine with all it's events, but in the second solution I have it does not update when new messages come in or when one should be displayed after sending, for instance.对我来说没有意义的是 RoomView 对所有事件的渲染都很好,但是在第二个解决方案中,例如,当有新消息进来或在发送后应该显示消息时,它不会更新。 I am passing a reference to the same Room object to it, but cannot for the life of me get it to update properly like it does in the first solution.我正在向它传递对同一个 Room 对象的引用,但在我的一生中无法像在第一个解决方案中那样正确更新它。
When I select a new user and go back to the previous one, the messages are all updated as expected.当我选择一个新用户并返回上一个用户时,消息都按预期更新。
Here is what I have tried so far:这是我迄今为止尝试过的:
Okay I finally figured this out, I have no idea why this works and it might be a dumb solution;好吧,我终于想通了,我不知道为什么会这样,这可能是一个愚蠢的解决方案; I needed to not only pass the selectedContact as a parameter, but also the room as another parameter.我不仅需要将 selectedContact 作为参数传递,还需要将房间作为另一个参数传递。 The code in the outline isnt exactly as it is in my source, but if you ever run into a problem where a class variable isnt updating properly in a view try to pass the variable down from higher up in the chain.大纲中的代码与我的源代码中的代码不完全相同,但是如果您遇到类变量未在视图中正确更新的问题,请尝试从链中的较高位置向下传递变量。
Which version of Xcode are you running?您运行的是哪个版本的 Xcode? If you are running Xcode 12.2 beta2, I'd recommend you to try it with Xcode 12.0.如果您运行的是 Xcode 12.2 beta2,我建议您尝试使用 Xcode 12.0。
I saw a similar issue with my code.我在我的代码中看到了类似的问题。 After wasting a lot of hours, I finally figure it out that Xcode (I was runnig Xcode 12.2 beta2) has a bug.在浪费了很多时间之后,我终于发现 Xcode(我正在运行 Xcode 12.2 beta2)有一个错误。
SwiftUI: Updating an array item does not update the child UI immediately SwiftUI:更新数组项不会立即更新子 UI
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.