Why Do We Need to Specify Schedule?

Hola,

I have the following simple lines of code.

import UIKit
import Combine

class ViewController: UIViewController {
	// MARK: - Variables
	var cancellable: AnyCancellable?
	@Published var labelValue: String?
	
	
	// MARK: - IBOutlet
	@IBOutlet weak var textLabel: UILabel!
	
	
	// MARK: - IBAction
	@IBAction func actionTapped(_ sender: UIButton) {
		labelValue = "Jim is missing"
	}
	
	
	// MARK: - Life cycle
	override func viewDidLoad() {
		super.viewDidLoad()
		
		cancellable = $labelValue
			.receive(on: DispatchQueue.main)
			.assign(to: \.text, on: textLabel)
	}
}

I just wonder what is the point of specifying the main thread with .receive? If I comment out the receive line, the app will still run without a problem. Muchos thankos

Answered by Claude31 in 690780022

At the time of call, you are probably already on main thread, so nothing changes.

That's important in case you have changed thread before.

May read this detailed tutorial:

h t t p s : / / trycombine.com/posts/subscribe-on-receive-on/

Accepted Answer

At the time of call, you are probably already on main thread, so nothing changes.

That's important in case you have changed thread before.

May read this detailed tutorial:

h t t p s : / / trycombine.com/posts/subscribe-on-receive-on/

Why Do We Need to Specify Schedule?
 
 
Q