SwiftUI自定义视图的ViewBuilder不会在子类ObservedObject更新时重新呈现/更新

问题描述 投票:0回答:1

[我已经研究了这几天,搜寻了Swift和SwiftUI文档,SO,论坛等,似乎找不到答案。

这里是问题;

我有一个SwiftUI自定义视图,该视图对到远程资源的自定义API请求类进行一些状态确定。 View会显示加载状态和失败状态,并通过ViewBuilder传递其主体内容,这样,如果API中的状态成功并且资源数据已加载,它将显示页面内容。

问题是,子类ObservedObject更新时,ViewBuilder内容不会重新呈现。对象会根据UI进行更新(按下按钮等时),但UI不会重新渲染/更新以反映子类ObservedObject内的更改,例如,子类ObservedObject内数组后面的ForEach不会在以下情况下刷新数组内容更改。如果将其移出自定义视图,则ForEach会按预期工作。

我可以确认代码已编译并运行。观察者和debugPrint()的全文显示ApiObject正在正确更新状态,并且视图反映了ApiState的更改绝对正确。它只是ViewBuilder的Content。我认为这是因为ViewBuilder只会被调用一次。

EDIT:上面的段落应该是提示,ApiState正确更新,但是在将大量日志记录到应用程序中之后,UI并未监听子类ObservedObject的发布。属性在变化,状态也在变化,但是UI对此没有反应。同样,下一个句子被证明是错误的,我在VStack中再次进行了测试,并且该组件仍然没有重新渲染,这意味着我在错误的位置查看!

如果是这种情况,VStack和其他此类元素如何解决?还是因为我的ApiObjectView在状态更改时被重新渲染而导致子视图“重置”?尽管在这种情况下,我希望它可以接管新数据并按预期工作,但它永远不会重新呈现。

有问题的代码在下面的CustomDataList.swiftApiObjectView.swift中。我留下的评论指向正确的方向。

这里是示例代码;

// ApiState.swift
// Stores the API state for where the request and data parse is currently at.
// This drives the ApiObjectView state UI.

import Foundation

enum ApiState: String
{
    case isIdle

    case isFetchingData
    case hasFailedToFetchData

    case isLoadingData
    case hasFailedToLoadData

    case hasUsableData
}
// ApiObject.swift
// A base class that the Controllers for the app extend from.
// These classes can make data requests to the remote resource API over the
// network to feed their internal data stores.

class ApiObject: ObservableObject
{
    @Published var apiState: ApiState = .isIdle

    let networkRequest: NetworkRequest = NetworkRequest(baseUrl: "https://api.example.com/api")

    public func apiGetJson<T: Codable>(to: String, decodeAs: T.Type, onDecode: @escaping (_ unwrappedJson: T) -> Void) -> Void
    {
        self.apiState = .isFetchingData

        self.networkRequest.send(
            to: to,
            onComplete: {
                self.apiState = .isLoadingData

                let json = self.networkRequest.decodeJsonFromResponse(decodeAs: decodeAs)

                guard let unwrappedJson = json else {
                    self.apiState = .hasFailedToLoadData
                    return
                }

                onDecode(unwrappedJson)

                self.apiState = .hasUsableData
            },
            onFail: {
                self.apiState = .hasFailedToFetchData
            }
        )
    }
}
// DataController.swift
// This is a genericised example of the production code.
// These controllers build, manage and serve their resource data.
// Subclassed from the ApiObject, inheriting ObservableObject

import Foundation
import Combine

class CustomDataController: ApiObject
{
    @Published public var customData: [CustomDataStruct] = []

    public func fetch() -> Void
    {
        self.apiGetJson(
            to: "custom-data-endpoint ",
            decodeAs: [CustomDataStruct].self,
            onDecode: { unwrappedJson in
                self.customData = unwrappedJson
            }
        )
    }
}

这是在ForEach更改为绑定数组属性时重新呈现其ObservedObject的视图。

// CustomDataList.swift
// This is the SwiftUI View that drives the content to the user as a list
// that displays the CustomDataController.customData.
// The ForEach in this View 

import SwiftUI

struct CustomDataList: View
{
    @ObservedObject var customDataController: CustomDataController = CustomDataController()

    var body: some View
    {
        ApiObjectView(
            apiObject: self.customDataController,
            onQuit: {}
        ) {
            List
            {
                Section(header: Text("Custom Data").padding(.top, 40))
                {
                    ForEach(self.customDataController.customData, id: \.self, content: { customData in
                        // This is the example that doesn't re-render when the
                        // customDataController updates its data. I have
                        // verified via printing at watching properties
                        // that the object is updating and pushing the
                        // change.

                        // The ObservableObject updates the array, but this ForEach
                        // is not run again when the data is changed.

                        // In the production code, there are buttons in here that
                        // change the array data held within customDataController.customData.

                        // When tapped, they update the array and the ForEach, when placed
                        // in the body directly does reflect the change when
                        // customDataController.customData updates.
                        // However, when inside the ApiObjectView, as by this example,
                        // it does not.

                        Text(customData.textProperty)
                    })
                }
            }
            .listStyle(GroupedListStyle())
        }
        .navigationBarTitle(Text("Learn"))
        .onAppear() {
            self.customDataController.fetch()
        }
    }
}

struct CustomDataList_Previews: PreviewProvider
{
    static var previews: some View
    {
        CustomDataList()
    }
}

这是有问题的自定义视图,不会重新呈现其内容。

// ApiObjectView
// This is the containing View that is designed to assist in the UI rendering of ApiObjects
// by handling the state automatically and only showing the ViewBuilder contents when
// the state is such that the data is loaded and ready, in a non errornous, ready state.
// The ViewBuilder contents loads fine when the view is rendered or the state changes,
// but the Content is never re-rendered if it changes.
// The state renders fine and is reactive to the object, the apiObjectContent
// however, is not.

import SwiftUI

struct ApiObjectView<Content: View>: View {
    @ObservedObject var apiObject: ApiObject

    let onQuit: () -> Void

    let apiObjectContent: () -> Content

    @inlinable public init(apiObject: ApiObject, onQuit: @escaping () -> Void, @ViewBuilder content: @escaping () -> Content) {
        self.apiObject = apiObject
        self.onQuit = onQuit
        self.apiObjectContent = content
    }

    func determineViewBody() -> AnyView
    {
        switch (self.apiObject.apiState) {
            case .isIdle:
                return AnyView(
                    ActivityIndicator(
                        isAnimating: .constant(true),
                        style: .large
                    )
                )

            case .isFetchingData:
                return AnyView(
                    ActivityIndicator(
                        isAnimating: .constant(true),
                        style: .large
                    )
                )

            case .isLoadingData:
                return AnyView(
                    ActivityIndicator(
                        isAnimating: .constant(true),
                        style: .large
                    )
                )

            case .hasFailedToFetchData:
                return AnyView(
                    VStack
                    {
                        Text("Failed to load data!")
                            .padding(.bottom)

                        QuitButton(action: self.onQuit)
                    }
                )

            case .hasFailedToLoadData:
                return AnyView(
                    VStack
                    {
                        Text("Failed to load data!")
                            .padding(.bottom)

                        QuitButton(action: self.onQuit)
                    }
                )

            case .hasUsableData:
                return AnyView(
                    VStack
                    {
                        self.apiObjectContent()
                    }
                )
        }
    }

    var body: some View
    {
        self.determineViewBody()
    }
}

struct ApiObjectView_Previews: PreviewProvider {
    static var previews: some View {
        ApiObjectView(
            apiObject: ApiObject(),
            onQuit: {
                print("I quit.")
            }
        ) {
            EmptyView()
        }
    }
}

现在,如果不使用ApiObjectView并将其内容直接放置在视图中,则以上所有代码都可以正常工作。

但是,这对于代码重用和体系结构来说太可怕了,这样虽然好看又整洁,但是不起作用。

是否还有其他方法可以解决此问题,例如通过ViewModifierView扩展名?

对此的任何帮助将不胜感激。

正如我所说,我似乎找不到任何人遇到此问题,也找不到任何在线资源可以为我指出正确的方向来解决此问题,或者可能是由什么引起的,例如ViewBuilder文档中概述的。] >

编辑:要添加一些有趣的东西,我此后向CustomDataList添加了一个倒数计时器,该计时器每1秒更新一次标签。 IF

文本由该计时器对象更新,视图被重新渲染,但是ONLY当标签上显示倒计时时间的文本被更新时。

我已经研究了这几天,搜寻了Swift和SwiftUI文档,SO,论坛等,似乎找不到答案。这是问题所在;我有一个SwiftUI自定义视图,它可以执行...

ios swift swiftui observedobject viewbuilder
1个回答
0
投票

[拔出我的头发一周后就发现了,这是一个未记录的问题,因为将ObservableObject分为子类,如此SO answer所示。

© www.soinside.com 2019 - 2024. All rights reserved.