Skip to content

Instantly share code, notes, and snippets.

@below
Last active February 6, 2021 09:57
Show Gist options
  • Save below/00a3f1606865413c4433eee4866f81a7 to your computer and use it in GitHub Desktop.
Save below/00a3f1606865413c4433eee4866f81a7 to your computer and use it in GitHub Desktop.
Is this good Combine Code?
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