Can I call static method from background queue in swift? Can I call static method from background queue in swift? multithreading multithreading

Can I call static method from background queue in swift?


You can use Closures for that

class ServerCommunication{    class func getData(url: String, sessionId: String, success: @escaping ((_ responseObject: ServerResponse?) -> Void)) {        //server communication code goes here        success(serverData) // serverData is a server response    }}func populateData() {    ServerCommunication.getData(url: "", sessionId: "", success: { (response) in       // You can handle response here       print(response)    })}


There are several reasons of any impact I could imagine:
1. At some point your getData modifies UI, which I believe isn't case here.
2. getData modifies any variables outside. Thus you need to wrap these modifications in synchronous queue.
3. getData calls other mutating methods. The solution is same as in 2.
Otherwise it's safe to call your static method from anywhere. The Singleton approach is not necessary, unless you need additional setup for your calls.


Yes you can call static method in background thread.

ClassName.performSelector(inBackground: #selector(self.ClassMethod), with: nil)