Vis*_*l16 8 multithreading background ios swift
我getData()在ServerCommunication类中有一个静态方法可以从后台队列中调用此方法.
//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: "")
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释对线程执行有什么影响或者我可能需要将ServerCommunication类定义为Singleton?
从后台队列调用时多次执行getData()静态类
#Edit1更多解释
当我尝试在viewController发生推送通知时打开特定时,会发生此问题.我正在使用一个名为FAPanelController的第三方库,它分别接受一个center,left和right viewController.
代码示例:
@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()
})
}
}
Run Code Online (Sandbox Code Playgroud)
小智 -3
是的,您可以在后台线程中调用静态方法。
ClassName.performSelector(inBackground: #selector(self.ClassMethod), with: nil)
Run Code Online (Sandbox Code Playgroud)