导航到另一个 xib 时未在 AppKit(主)线程问题上运行
Posted
技术标签:
【中文标题】导航到另一个 xib 时未在 AppKit(主)线程问题上运行【英文标题】:not running on AppKit (main) thread issue when navigating to another xib 【发布时间】:2018-09-05 09:42:36 【问题描述】:我的应用程序的用例是有一个在SafariExtensionViewController.xib
中处理的登录用户界面,当用户点击登录按钮时,必须显示另一个用户界面,即ExtensionStepsViewController.xib
。我使用以下导航到另一个屏幕
self.presentViewControllerAsSheet(ExtensionStepsViewController())
但是,我遇到了以下问题
void assertRunningOnAppKitThread(void)() 中的断言失败,2018-09-05 15:02:30.318742+0545 扩展 [13150:1422110] [General] 不在 AppKit(主)线程上运行
这是我的代码
SafariExtensionViewController.swift
import SafariServices
class SafariExtensionViewController: SFSafariExtensionViewController
@IBOutlet weak var passwordMessage: NSTextField!
@IBOutlet weak var emailMessage: NSTextField!
@IBOutlet weak var message: NSTextField!
@IBOutlet weak var email: NSTextField!
@IBOutlet weak var password: NSSecureTextField!
static let shared = SafariExtensionViewController()
override func viewDidLoad()
message.stringValue = ""
emailMessage.stringValue = ""
passwordMessage.stringValue = ""
@IBAction func userLogin(_ sender: Any)
let providedEmailAddress = email.stringValue
let providedPassword = password.stringValue
let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress)
self.message.stringValue = ""
emailMessage.stringValue = ""
passwordMessage.stringValue = ""
if isEmailAddressValid && providedPassword.count > 0
let session = URLSession.shared
var request = URLRequest(url: URI!)
request.httpMethod = "POST"
do
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
catch let error
print(error.localizedDescription)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = session.dataTask(with: request as URLRequest, completionHandler: data, response, error in
guard error == nil else
return
guard let data = data else
return
do
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]
if let httpResponse = response as? HTTPURLResponse
if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300)
print("#########LOGIN########################")
self.message.stringValue = "Login Successful"
self.message.textColor = NSColor.green
UserDefaults.standard.set(providedEmailAddress, forKey: "email")
self.presentViewControllerAsSheet(ExtensionStepsViewController()) // error is thrown after this action
else
self.message.stringValue = "Invalid Credentials"
self.message.textColor = NSColor.red
// handle json...
catch let error
print(error.localizedDescription)
)
task.resume()
else
emailMessage.textColor = NSColor.red
emailMessage.stringValue = "Invalid Email"
ExtensionStepsViewController.swift
import SafariServices
class ExtensionStepsViewController: SFSafariExtensionViewController
// static let shared = SafariExtensionViewController()
override func viewDidLoad()
super.viewDidLoad()
print("Hello") // this is also printed
// Do view setup here.
当登录成功,然后这个语句被执行self.presentViewControllerAsSheet(ExtensionStepsViewController())
print("hello")
被打印在调试部分,我得到了列出的问题。
【问题讨论】:
【参考方案1】:UI 的所有更新都必须在主线程中完成。好像主线程没有调用completionHandler
,导致这个断言失败。
您需要在主队列中进行演示。您可以通过如下安排一个块来做到这一点:
DispatchQueue.main.async
// This will now run on the main queue.
self.presentViewControllerAsSheet(ExtensionStepsViewController())
【讨论】:
我不知道这个是否有效,但问题现在不存在。它可以导航,但窗口会在一秒钟内关闭。 那是因为没有人持有对该对象的强引用——它正在被释放以上是关于导航到另一个 xib 时未在 AppKit(主)线程问题上运行的主要内容,如果未能解决你的问题,请参考以下文章
推送 viewController 时未调用 viewDidLoad
为啥当我使用 presentModelViewController 时未在我的 UITabBArController 中调用 viewWillAppear?