//ServerCommunication.swiftimport UIKitimport Foundationclass 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: "") }}
任何人都可以解释对线程执行有什么影响或者我可能需要将ServerCommunication类定义为Singleton?
getData() static class executed multiple times when calling from background queue
#Edit1更多解释
当我尝试在发生推送通知时打开特定的vIEwController时发生此问题.我正在使用一个名为FAPanelController的第三方库,它分别接受一个中心,左右vIEwController.
代码示例:
@available(iOS 10.0,*)func userNotificationCenter(_ center: UNUserNotificationCenter,dIDReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaPing () -> VoID) {//Now I want to open a vIEwControllerif 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() })}}解决方法 您可以使用Closures
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) })}总结
以上是内存溢出为你收集整理的ios – 我可以在swift中从后台队列中调用静态方法吗?全部内容,希望文章能够帮你解决ios – 我可以在swift中从后台队列中调用静态方法吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)