0

I have scenario where threads calling the same method which is having timer, for each thread timer will be different because thread created in different timeline based on the action from the UI. How can I get the timer value for each thread separately in single view and single init of the class, though I tried to get the different response but for dynamic creation of threads its not responsive, below one I am just trying on playground

var groupqueue = DispatchGroup()
private var update: [AnyCancellable] = []
var ticker: Int = 0

groupqueue.enter()
multiplecall(name: "test1")

groupqueue.enter()
multiplecall(name: "test2")

func multiplecall(name: String){
    Timer.publish(every: 1, on: .main, in: .common)
        .autoconnect()
        .sink { _ in fetchValues() }
        .store(in: &update)

}

func fetchValues(){
        ticker += 1
        print(ticker)
    }

required output : 1 1, 2 2, 3 3...\n

what I am getting: 1,2,3,4...

OhStack
  • 106
  • 1
  • 1
  • 15
  • 1
    Try an reformat for `async await` and get rid of the `Timer`. But you likely need 2 `ticker` variables no matter what – lorem ipsum Jun 14 '23 at 12:12

1 Answers1

1

Your code sample is incomplete. Some of the variables aren't declared, and it's not clear why you are trying to use a dispatch group, the name you pass to multicall is not used. There are a number of problems.

I copied your code into a Playground and edited it as follows:

import UIKit
import Combine

var subscriptions = Set<AnyCancellable>()

func multiplecall(name: String) {
    Timer.publish(every: 1, on: .main, in: .common)
        .autoconnect()
        .scan(1) { value, _ in value + 1 }
        .sink(receiveValue: fetchValues)
        .store(in: &subscriptions)
}

func fetchValues(ticker: Int){
    print(ticker)
}

multiplecall(name: "test1")
multiplecall(name: "test2")

This produces the output you say you are expecting.

I note that this code does not appear to be multithreaded. The timer is set to publish on the main thread so all of the code in this sample will be run on the main thread. The timers themselves might be running on separate threads (I haven't ever looked to see how Timer is implemented) but the timer events are delivered to the main run loop.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • its working, I have just tried to recreate it in playground. By the way threads created with Thread() class and there will two timer tickers with different time interval. Kindly suggest about creating thread? GCD,opreationQueue or raw threads and if its with swiftUI then – OhStack Jun 14 '23 at 14:11
  • 1
    If you are just running timers then you may not need explicit threads. Which you would use really depends on what you are trying to do. I would look at using Swift Concurrency (aka. async/await) first. Then `Combine`, then DispatchQueues and DispatchSource timers. If none of those met my needs then maybe Operation Queues would work. I can't really think of a good reason to go to POSIX threads for most application uses. – Scott Thompson Jun 14 '23 at 18:53
  • @ScottThomson, how can I scan two values like. func fetchValues(testName: String, ticker: Int){ print(testName,ticker) dict[testName] = ticker } – OhStack Jun 19 '23 at 09:29