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()
})
}
}
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.
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.
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.
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.
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)
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With