Swift Combine, Avoid nested subscribers on optionals

Viewed 910

I have an AccountService class that holds a User object. The user object gets set async from a network request. In my UI I'm displaying the user and want to keep changes up to date. I'm using the Swift Combine Framework to do this.

Question: Is there a way to avoid nested subscribers on an object?

I wrote some test code to illustrate this:

import UIKit
import Combine

class User: ObservableObject, CustomDebugStringConvertible {
    @Published
    var name: String

    init(name: String) {
        self.name = name
    }

    var debugDescription: String {
        return self.name
    }
}

class AccountService {

    @Published
    var user: User? = nil

    var userCancel: AnyCancellable?
    var userContentCancel: AnyCancellable?
    init() {
        self.userCancel = self.$user.sink { (user) in
            print("Set user: \(String(describing: user))")

            guard let user = user else { return }
            self.userContentCancel = user.$name.sink { _ in
                print("new name: \(String(describing: self.user))")
            }
        }
    }

    func setUseru(user: User) {
        self.user = user
    }

    func changeUserName(name: String) {
        self.user?.name = name
    }
}

let x = AccountService()
x.setUseru(user: User(name: "Philipp"))
x.changeUserName(name: "Tom")
x.setUseru(user: User(name: "Anna"))

Run in Playgroud Xcode Version 11.4.1, Swift 5

Output

Set user: nil
Set user: Optional(Philipp)
new name: nil
new name: Optional(Philipp)
Set user: Optional(Anna)
new name: Optional(Tom)

Ideally I'd like only to listen to self.$user.sink for when the object is set AND for when the content of the object changes. I've played around with self.objectWillChange.send() when setting the username, but I'm not able to trigger the outer publisher.

I'm looking for a way to get rid of

guard let user = user else { return }
self.userContentCancel = user.$name.sink { _ in
    print("new name: \(String(describing: self.user))")
}

in my implementation and just drive everything from the same self.$user.sink { (user) in implementation.

1 Answers

Yes, there is a way. You can simply use Combine's flatMap operator:

Transforms all elements from an upstream publisher into a new publisher up to a maximum number of publishers you specify.

My working example:

import Foundation
import Combine

class User: ObservableObject {
    @Published var name: String

    init(name: String) {
        self.name = name
    }

    var debugDescription: String {
        return self.name
    }
}

class AccountService {
    @Published var user: User? = nil
    var userCancel: AnyCancellable?
    
    init() {
        self.userCancel = self.$user
            .filter { $0 != nil }
            .flatMap { $0!.$name }
            .sink(receiveValue: { name in
                print(name)
            })
    }

    func setUseru(user: User) {
        self.user = user
    }

    func changeUserName(name: String) {
        self.user?.name = name
    }
}

let x = AccountService()
x.setUseru(user: User(name: "Philipp"))
x.changeUserName(name: "Tom")
x.setUseru(user: User(name: "Anna"))

The program's output will be:

Philipp
Tom
Anna
Related