Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call static method from background queue in swift?

I have a static getData() method in the ServerCommunication class can I call this method from background queue.

//ServerCommunication.swift

import UIKit
import Foundation


class ServerCommunication
{
      class func getData(url: String, sessionId: String) -> ServerResponse {
            //server communication code goes here
      }
}

func populateData() {
     DispatchQueue.global(qos: .background).async {
           let response = ServerCommunication.getData(url: URL, sessionId: "")
     }
}

Can anybody explain what will the impact on thread executions Or may I need to define ServerCommunication class as Singleton?

getData() static class executed multiple times when calling from background queue

#Edit1 Some more explanations

This problem occurred when I am trying to open a specific viewController when Push notification occurs. I am using a third party library called FAPanelController which accept a center, left and right viewController respectively.

Code sample:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//Now I want to open a viewController
if let panel = self.window?.rootViewController as? FAPanelController     
{
    let centerNavVC = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! UINavigationController
        let vc = centerNavVC.topViewController as! HomeViewController
        panel.center(centerNavVC, afterThat: {
            //HomeViewController has a method populateData() in viewWillAppear()
        })
}
}
like image 369
iamVishal16 Avatar asked May 18 '18 11:05

iamVishal16


People also ask

What is static method in Swift?

A static method is of class type (associated with class rather than object), so we are able to access them using class names. Note: Similarly, we can also create static methods inside a struct. static methods inside a struct are of struct type, so we use the struct name to access them.

What is main thread and background thread in Swift?

Main Thread & Background Thread: If we call the background thread function on our main thread it will freeze the UI (user interface) & the user won't be able to perform any interactions with the app that will make him think that the app is freezed.

What is Dispatchqueue in Swift?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.

What is a static class Swift?

Swift allows us to use a static prefix on methods and properties to associate them with the type that they're declared on rather than the instance. We can also use static properties to create singletons of our objects which, as you have probably heard before is a huge anti-pattern.


1 Answers

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)
    })
}
like image 166
Jogendar Choudhary Avatar answered Sep 27 '22 22:09

Jogendar Choudhary