SwiftUI观察更改

时间:2020-04-16 11:49:29

标签: swift swiftui combine observableobject

概述:

  • 我有一个名为Player的班级和一个名为Song的班级。
  • 玩家包含一首歌曲
  • 一个视图正在显示歌曲标题

目标:

当我更改player.song.title时,视图需要更新。

问题:

当歌曲的属性更改时,它不会自动更新视图。仅当分配了新歌曲时,更改才会反映出来。

我的尝试:

我做了2次尝试(下面的代码),都按预期工作。

问题:

  • 有更好的方法吗? (这似乎是一个常见问题,可能会遇到。) 我的尝试合理吗?尝试2更好吗?
  • 或者我的设计存在根本性缺陷吗? (我希望将歌曲包含在播放器中,因为它代表了当前歌曲)。

原始代码(视图不会更新):

型号

import Foundation
import Combine

class Player : ObservableObject {

    @Published var duration = 0
    @Published var song     : Song

    init(song: Song) {
        self.song = song
    }
}

class Song : ObservableObject {

    @Published var id     : Int
    @Published var title  : String
    @Published var artist : String

    init(id: Int,
         title: String,
         artist: String) {

        self.id     = id
        self.title  = title
        self.artist = artist
    }
}

查看

import SwiftUI

struct ContentView: View {

    @ObservedObject var player : Player

    var body: some View {

        VStack {
            Text(String(player.duration))
            Text(player.song.title)
            Text(player.song.artist)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

        let song = Song(id: 1, title: "title1", artist: "artist1")
        let player = Player(song: song)

        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            player.song.title = "title2"
        }

        return ContentView(player: player)
    }
}

Attempt1-使用CombineLatest

问题:随着歌曲中属性的数量增加,它的伸缩性不是很好。

class Player : ObservableObject {

    @Published var duration = 0
    @Published var song     : Song
    private var songChangeCanceller : AnyCancellable?

    init(song: Song) {
        self.song = song

        songChangeCanceller = song.$title.combineLatest(song.$artist, song.$id).sink { _, _, _ in
            self.objectWillChange.send()
        }
    }
}

Attemp2:使用objectWillChange.sink

class Player : ObservableObject {

    @Published var duration = 0
    @Published var song     : Song

    private var songChangeCanceller : AnyCancellable?
    private var songAttributesChangeCanceller : AnyCancellable?

    init(song: Song) {
        self.song = song

        songChangeCanceller = $song.sink { newSong in

            self.songAttributesChangeCanceller = newSong.objectWillChange.sink { _ in
                self.objectWillChange.send()
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

只需简化以下模型,更新就可以工作。使用Xcode 11.4 / iOS 13.4进行了测试

class Player : ObservableObject {

    @Published var duration = 0
    @Published var song: Song // published as soon as song.title changed

    init(song: Song) {
        self.song = song
    }
}

struct Song : Identifiable { // value type
    var id     : Int
    var title  : String
    var artist : String
}

答案 1 :(得分:1)

我想知道宋是否是一堂课,我会怎么做

好吧,如果我们限于类,那么解决方案将采用简化设计,如下所示(是的,我更喜欢简单的解决方案)。在Xcode 11.4 / iOS 13.4上进行了测试。

// << keep your model "as is" in section "Model", instead modify views

struct ContentView: View {
    @ObservedObject var player : Player

    var body: some View {
        VStack {
            Text(String(player.duration))
            SongDetailsView(song: player.song)
        }
    }
}

struct SongDetailsView: View {
    @ObservedObject var song : Song
    var body: some View {
        VStack {
            Text(song.title)
            Text(song.artist)
        }
    }
}