为啥当代码从 Alamofire 响应中触发时 KYDrawerController 不显示?

Posted

技术标签:

【中文标题】为啥当代码从 Alamofire 响应中触发时 KYDrawerController 不显示?【英文标题】:Why KYDrawerController do not show when code fired from Alamofire response?为什么当代码从 Alamofire 响应中触发时 KYDrawerController 不显示? 【发布时间】:2019-03-07 16:38:12 【问题描述】:

我正在使用 KYDrawerController 在登录成功时在主屏幕上显示侧边菜单。

我使用 Alamofire 请求用户的身份验证,当响应成功时,然后用抽屉打开主屏幕。

问题是主屏幕不显示或登录屏幕保持静止,但是当从 Alamofire 响应之外调用代码时,它工作得很好。 这是我的代码

Alamofire.request(URL_LOGIN_PHONE, method: .post, parameters: parameters).responseJSON   response in

        switch response.result 
        case .success(let value):
            let jsonResponse = JSON(value)

            // Check if status = success
            if jsonResponse["status"] == "success" 

        // Init Drawer
        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        let storyBoard = UIStoryboard.init(name: "Main", bundle: Bundle.main)

        let navVC = storyBoard.instantiateViewController(withIdentifier: "NavVC")  // is the main controller for the drawer.
        let drawerVC = storyBoard.instantiateViewController(withIdentifier: "DrawerVC")  // is the drawer, and the drawer needs a main controller

        appDelegate.drawerController.mainViewController = navVC
        appDelegate.drawerController.drawerViewController = drawerVC

        appDelegate.window?.rootViewController = appDelegate.drawerController
        appDelegate.window?.makeKeyAndVisible()

             else 

            

        case .failure( _):
        

    

【问题讨论】:

【参考方案1】:

你必须把代码放在异步闭包块中,像这样

DispatchQueue.main.async 
 // Init Drawer
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    let storyBoard = UIStoryboard.init(name: "Main", bundle: Bundle.main)

    let navVC = storyBoard.instantiateViewController(withIdentifier: "NavVC")  // is the main controller for the drawer.
    let drawerVC = storyBoard.instantiateViewController(withIdentifier: "DrawerVC")  // is the drawer, and the drawer needs a main controller

    appDelegate.drawerController.mainViewController = navVC
    appDelegate.drawerController.drawerViewController = drawerVC

    appDelegate.window?.rootViewController = appDelegate.drawerController
    appDelegate.window?.makeKeyAndVisible()

【讨论】:

有没有办法知道抽屉是否打开了? 在 rootViewController 行之前添加 self.window = UIWindow(frame: UIScreen.main.bounds) 窗口和drawerController在AppDelegate中声明。 对不起,我的意思是这一行appDelegate.window = UIWindow(frame: UIScreen.main.bounds) 在 rootViewController 行上方 感谢您的宝贵时间,但我找到了解决方案。【参考方案2】:

问题与 Alamofire 或主线程无关,只是代码试图实例化已加载的抽屉。所以我添加了代码来检查它是否加载。如果已经加载,我使用 unwindSegue 返回主视图控制器。

let appDelegate = UIApplication.shared.delegate as! AppDelegate
        // check if drawer loaded or not.
        if appDelegate.drawerController.isViewLoaded 
            // Drawer loaded prevoiusly, dont load it again
            appDelegate.drawerController.setDrawerState(.closed, animated: true)
            self.exitToRestaurants()
         else 
            // Drawer not loaded from prevouis use.
            // and it will be loaded for the first time/
            let storyBoard = UIStoryboard.init(name: "Main", bundle: Bundle.main)

            let navVC = storyBoard.instantiateViewController(withIdentifier: "NavVC")  // is the main controller for the drawer.
            let drawerVC = storyBoard.instantiateViewController(withIdentifier: "DrawerVC")  // is the drawer, and the drawer needs a main controller


            appDelegate.drawerController.mainViewController = navVC
            appDelegate.drawerController.drawerViewController = drawerVC

            appDelegate.window?.rootViewController = appDelegate.drawerController
            appDelegate.window?.makeKeyAndVisible()
        

【讨论】:

以上是关于为啥当代码从 Alamofire 响应中触发时 KYDrawerController 不显示?的主要内容,如果未能解决你的问题,请参考以下文章

为啥以这种方式使用 Alamofire 时没有发现任何价值?

在 Alamofire 中检测缓存的响应

尝试从响应 Swift Alamofire 将数据保存到核心数据时崩溃

响应中缺少 Alamofire 参数

Alamofire 响应处理程序的问题

为啥我无法从 Alamofire 获取请求结果