列表未在swiftUI中加载。更新

时间:2020-03-12 09:17:40

标签: ios swiftui

我使用AlamoFire从服务器获取了一些数据,请求很好,模型一切都很好,问题是,请求完成后,我的@State变量不会更新。

import SwiftUI
import Combine

struct ViewLaundryHome: View {

    @State var items = LaundryItemCategoryViewModel()

    var body: some View {
        VStack {
            Text(items.items.count.description) // returns 0 and never updates
        }
    }
}

class LaundryItemCategoryViewModel: ObservableObject {

    var didChange = PassthroughSubject<LaundryItemCategoryViewModel, Never>()

    var items = [LaundryItemCategory.Result.Doc]() { didSet { didChange.send(self) } }

    init() {
        productCategoryR {
            let d = try! JSONDecoder().decode(LaundryItemCategory.self, from: $0)
            self.items = (d.result?.docs!)!
            print(self.items.count) // returns 5
        }
    }
}

我想我做的和YouTube上的视频一样,但是对我来说不起作用。

https://www.youtube.com/watch?v=xT4wGOc2jd4&t=17s

这是我的数据模型:

struct LaundryItemCategory: Codable {
    let result: Result?
    let id: Int?

    enum CodingKeys: String, CodingKey {
        case result = "Result"
        case id = "Id"
    }

    struct Result: Codable {
        let docs: [Doc]?
        let currentPage: Int?
        let pages: Int?
        let limit: Int?
        let total: Int?

        enum CodingKeys: String, CodingKey {
            case docs = "Docs"
            case currentPage = "CurrentPage"
            case pages = "Pages"
            case limit = "Limit"
            case total = "Total"
        }

        struct Doc: Codable, Hashable, Identifiable {
            let id: Int
            let title: String
            let media: String
            let price: Int

            enum CodingKeys: String, CodingKey {
                case id = "Id"
                case title = "Title"
                case media = "Media"
                case price = "Price"
            }
        }
    }
}

之后。一些挖掘:

发生了一些奇怪的事情。我在应用程序中添加了另一个状态,并使用文本字段对其进行了更改,正是在我更改新状态的值的那一刻,第一个状态更新了:-/

@State var items = LaundryItemCategoryViewModel()

@State var i = ""

var body: some View {
    VStack {
        Text(items.items.count.description)
        TextField("sss", text: $i)
        Text(i)
    }
}

1 个答案:

答案 0 :(得分:2)

就像我说的那样,您需要在ViewLaundryHome中将项目作为ObservedObject接收更新。在LaundryItemCategoryViewModel中,您需要发布项目。这是在iPhone 11 Pro Max 13.3和Xcode版本11.3.1中测试的工作代码。让我知道这是否有效。

import SwiftUI
import Combine

struct ViewLaundryHome: View {
    @ObservedObject var items = LaundryItemCategoryViewModel()

    var body: some View {
        VStack {
            Text(items.items.count.description)
        }
    }
}

class LaundryItemCategoryViewModel: ObservableObject {

    // var didChange = PassthroughSubject<LaundryItemCategoryViewModel, Never>() // Not needed

    @Published var items = [LaundryItemCategory.Result.Doc]() // Add Published
    // { didSet { didChange.send(self) } } - Not needed

    init() {
        // Delaying the creation of data to mimic data coming from a webserver
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0) {
            let x = LaundryItemCategory.Result.Doc(id: 1, title: "Test", media: "Test", price: 0)
            let y = LaundryItemCategory.Result.Doc(id: 2, title: "Test", media: "Test", price: 0)

            self.items.append(x)
            self.items.append(y)
        }

        print(self.items.count) // returns 5
    }
}