我正在尝试显示主视图seque的detailViewController。
appdelegate.swift的相关部分:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Notification: basic delegate (background fetch)")
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("2 Message ID: \(messageID)")
print("2 message : \(userInfo)")
openPostDetail(userInfo: userInfo)
}
completionHandler(UIBackgroundFetchResult.newData)
}
func openPostDetail(userInfo: [AnyHashable: Any]) {
let post_id = userInfo["post_id"] as? String
print("post_id: \(post_id) ")
let baseURL = URL(string: websiteUrl)
//89169?_embed
let postId = "89169"
let url = URL(string: "wp-json/wp/v2/posts/\(postId)?_embed", relativeTo: baseURL)
var newPost :Posts?
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
guard let data = data, error == nil else { return }
do {
let json: NSDictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! NSDictionary
newPost = Posts(post: json)
print("author: \(String(describing: newPost?.postAuthor))")
let sb = UIStoryboard(name: "Main", bundle: nil)
let rootVC = sb.instantiateViewController(withIdentifier: "mainView") as! NewsViewController
let otherVC = sb.instantiateViewController(withIdentifier: "postDetailIdn") as! PostDetailTableViewController
otherVC.post = newPost!
rootVC.navigationController?.pushViewController(otherVC, animated: true)
} catch let error as NSError {
print(error)
}
}).resume()
}
` 当我点击推送通知时,我收到此错误: 试图从主线程或Web线程以外的线程获取Web锁。这可能是从辅助线程调用UIKit的结果。现在崩溃......
提前谢谢
答案 0 :(得分:0)
您需要更新主线程上的UI。
DispatchQueue.main.async {
let sb = UIStoryboard(name: "Main", bundle: nil)
var navigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as! UINavigationController
let otherVC = sb.instantiateViewController(withIdentifier: "postDetailIdn") as! PostDetailTableViewController
otherVC.post = newPost!
navigationController?.pushViewController(otherVC, animated: true)
}