Combine `.receive(on: RunLoop.main)` loses sent value. How can I make it work?

Hi @Tony_Parker I am facing a similar issue, stated below is the example:

Xcode 11.2.1 (11B500)

Problem:

When .receive(on: RunLoop.main) is used sink doesn't receive any values, however when it is commented out, then sink receives values.

Note: I have tried the following but still doesn't work:

  • Retained cancellable
  • Tested on iOS Device / Simulator / Playground.

Code:

import Foundation
import Combine

extension Notification.Name {
    public static let didPost = Notification.Name("didPost")
}

func postNotifications() {
    
    let names = ["aaa", "bbb", "ccc"]
    
    for name in names {
        
        NotificationCenter.default.post(name: .didPost,
                                        object: nil,
                                        userInfo: ["Car" : name])
    }
}

let cancellable = NotificationCenter.default.publisher(for: .didPost)
    .compactMap { $0.userInfo?["Car"] as? String }
    .receive(on: RunLoop.main) //works when commented out
    .sink {
        print("sink: \($0)")
}

postNotifications()