Last active
February 6, 2021 09:57
-
-
Save below/00a3f1606865413c4433eee4866f81a7 to your computer and use it in GitHub Desktop.
Is this good Combine Code?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func behindThePublisher(url: URL) -> AnyPublisher<NSAttributedString, Never> { | |
/// … | |
} | |
class ActionViewController: UIViewController { | |
@IBOutlet weak var textView: UITextView! | |
var cancellables = Set<AnyCancellable>() | |
@Published var displayText: NSAttributedString = NSAttributedString() | |
func processURL(_ url: URL) { | |
behindThePublisher(url: url) | |
.sink { self.displayText = $0 } | |
.store(in: &self.cancellables) | |
/// I am storing the result in displayString, because I need it later | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// But so that the textView is automatically updated, I add a sink here | |
$displayText | |
.receive(on: DispatchQueue.main) | |
.sink { value in | |
self.textView.attributedText = value | |
} | |
.store(in: &cancellables) | |
/// … | |
processURL(url) | |
/// … | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment