0

Here is the Sample code

import Combine
import UIKit

class ViewController: UIViewController {
    @IBOutlet var Discover: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func buttonClicked(_ sender: Any) {
        demo().xyz()
        print("Hello1")
    }

    public class demo {
        func xyz() {
            discoveryHandlerQueue.async {
                var timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.timerAction), userInfo: nil, repeats: false)

                print("Hello")
            }
        }

        @objc func timerAction() {
            print("timer fired!")
        }
    }
}

I am not able to recieve timer callback here to print "timer fired!". Why is this happening.Moment I change it from async to sync everything runs smoothly

  • 1
    I believe this should answer your question: https://stackoverflow.com/a/52231474/4081597 – Rob C Apr 15 '20 at 11:33
  • Using above runLoop , it Timer becomes blocking .So until we don't recieve call back for timeout ,"Hello" isn't printed – Atishya Jain Apr 15 '20 at 13:49

1 Answers1

0

you can use dispatch_source_create method to create timer, this timer can set running queue

jianpx
  • 3,190
  • 1
  • 30
  • 26