SwiftUI环境对象函数onLoad导致工作表自动关闭

时间:2020-03-19 09:57:10

标签: swift swiftui swiftui-environment

我对environmentObject有问题,如果我评论onappear函数的工作原理,则在打开工作表时会自动关闭。

在我的工作表页面

@EnvironmentObject var networkManagerNewsWags: NetworkManagerNewsWags
    var body: some View {
        ......
        .onAppear {
           self.networkManagerNewsWags.getNewsById(id: self.data.id)
        }
    }

这是课程:

class NetworkManagerNews: ObservableObject {


    @Published var newsById: Array<News> = []

    @Published var loadingNewsById = false

    private let api_url_base = "mylink"

    init() {
       // loadingNewsById = true
    }


    func getNewsById(id: Int) {
        loadingNewsById = true
        guard let url = URL(string: "\(api_url_base)/getNewsByID/\(id)") else { return }
        URLSession.shared.dataTask(with: url){ (data, _, _) in
            guard let data = data else { return }
            let newsByWagsList = try! JSONDecoder().decode(Array<News>.self, from: data)
            DispatchQueue.main.async {
                self.newsById = newsByWagsList
                self.loadingNewsById = false
            }
        }.resume()
    }
}

谢谢!

0 个答案:

没有答案