'How to call published property without updating value

In SwiftUI, combine I have a published property.

@Published
var name: String

And when the name is updated it calls an app and set other values.

$name
...
...
.assign(to: &$something)

Now I want to call this without updating the name at some case



Solution 1:[1]

Are you trying to do something like this:

import UIKit
import Combine
import SwiftUI
import Foundation


class AddPassaround : ObservableObject {
    @Published var name: String = ""
    @Published var reversed : String = ""
    let passaround = PassthroughSubject<String, Never>()

    init() {
        $name
            .merge(with: passaround)
            .map { String($0.reversed()) }
            .assign(to: &$reversed)
    }


    func doIt() {
        $reversed.sink { print($0) }
        name = "reversedFromSettingName"
        print(name)
        passaround.send("reversedFromPassaround")
        print(name)
    }
}

AddPassaround().doIt()

This adds a second publisher called passaround and you can invoke the behavior after the $name by either publishing a value to name or by passing the value through the passaround publisher.

Solution 2:[2]

Answering my own question because I found something that provides the intended functionality. I simply updated the updateLoop() call in the example to this.

  async updateLoop() {
    while (!this.windowShouldClose()) {
      await null;
      this.onUpdate.emit();
      this.update();
    }
  }

Would somebody be able to comment with more clarification why this now produces the intended effect? Changing the function to async does not impact the point at which the promise resolves unless I add an await statement. My assumption that is including an await statement frees the event loop which finally allows the promise to resolve. If you edit the above JSfiddle with this function, the numbers printed in the console won't coincide with what the print statements "expect" - that only has to do with then the counter is incremented. The execution order appears to be correct.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scott Thompson
Solution 2 tuckie