@StateObject with @Published variable not updating SwiftUI

Welcome back to another edition of the man who cannot manage state in Swift!

In this addition, we are going to figure out why the View is not updating despite an @Published variable is being changed.

The exhibited behavior is that our APIResultView enters the .loading state but never exits it, despite us recieving data! A classic problem.

Here is our lovely feature code for this episode:

A VM that seems to be constructed correctly!

///View Model for the Admin page
class AdminVM : ViewModel {
    
    var community_id: Int = 0           //Current session's community identifier
    var profile_id: Int = 0            //Current session's profile identifier
    
    ///Pulled Notifications from database
    @Published var Notifications: ViewResult<[AdminNotification]> = ViewResult(defaultValue: [])
    
    ///API Dispatcher
    private var dispatcher : ViewModelAPIDispatcher = ViewModelAPIDispatcher()
    
    func Initialize(community_id: Int, profile_id: Int){
        self.community_id = community_id
        self.profile_id = profile_id
        self.OnViewOpen()
    }
    
    func OnViewOpen() {
        dispatcher.SendRequestViewUpdate(.getAdminNotifications(community_id: community_id)){ result in
            self.Notifications.SetData(result)
        }
    }
    
    func Refresh() {
        self.Notifications.Refresh()
        self.OnViewOpen()
    }

The view it is attached to…

struct AdminView: View {
    
    private var community_id = 0
    private var profile_id = 0
    @StateObject private var VM = AdminVM()
    
    init(community_id: Int, profile_id: Int) {
        self.community_id = community_id
        self.profile_id = profile_id
    }
    
    
    var body: some View {
        APIResultView(apiResult: $VM.Notifications) { notifications in
            //Success display code
            }
        }.refreshable {
            VM.Refresh()
        }.onAppear(){
            VM.Initialize(community_id: community_id, profile_id: profile_id)
        }
    }

and the view containing logic of what is being displayed!

struct APIResultView<Content : View, T>: View {
        
        @Binding var apiResult : ViewResult<T>      //Actual data being displayed
        var successDisplay : (T) -> Content       //What to display if suscessful
        
        //What to display if we have an object, are waiting, or get a code back.
        var body: some View {
            switch apiResult.state{
            case .object:
                successDisplay(apiResult.data!)
            case .loading:
                ProgressView("Loading...")
               
            case .code(let code):
                HttpStatusView(statusCode: code)
            }
        }
}

We see some familiar faces here, so lets see what we’ve tried.

Initially, I was initilizing my state object as with the following constructor within in the View init():
StateObject(wrappedValue: AdminVM(...))

I read on the docs that this could lead to unexpected behavior, so now it is initialized in the onAppear() of the view.

I also have breakpointed to ensure that .SetData() for the ViewResult is being called, and it is.

Lastly, I’ve found that if I add an @State variable to the view, our characters behave as expected, which is bonkers! If someone could explain why that is, that would be wonderful.

It is also worthy to that this design works with in my many other, numerous views, but that have more complex state, so I’ve not run into this until now it seems.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật