使用脱机存储时禁用了用户交互

时间:2017-03-22 03:33:26

标签: swift uitableview nsuserdefaults

我已将我的json响应存储在 NSUserdefaults 中,我必须显示存储的响应,即使数据处于脱机状态,结果还可以 但离线时禁用ui的用户交互。 希望能有人帮忙

我试过通过给出网络可达性的断点来连接或不进入循环并显示响应但是ui交互被触发

func homePageAPI()
{
       let kUserDefault = UserDefaults.standard

       if Reachability.isConnectedToNetwork() == true{
       APIModel().homeApi(withUrl: "", withParameters:"", withComplitionBlock: {(_ responseDict: Dictionary!, _ error:String) -> Void in// here im not displaying my url as it is illegal

           DispatchQueue.main.async (execute:{ () -> Void in
           if (responseDict != nil)
           {
                  kUserDefault.set(responseDict, forKey: "responseDict")
                  self.productresponseArray = (responseDict["server_productresponse"] as AnyObject) as! NSArray
                  self.productDetailsArray = ((responseDict["server_productresponse"] as AnyObject) .value(forKey: "product_dtls") as AnyObject) as! NSArray
                  self.vehicleStatusStringArray = ((responseDict["server_productresponse"] as AnyObject) .value(forKey: "veh_status") as AnyObject) as! NSArray
           }
           })
        })
      }
      else{
            let offlineResponseDict:NSDictionary = kUserDefault.value(forKey: "responseDict") as! NSDictionary
            print(offlineResponseDict as Any)
            self.productresponseArray = (offlineResponseDict["server_productresponse"] as AnyObject) as! NSArray
            self.productDetailsArray = ((offlineResponseDict["server_productresponse"] as AnyObject) .value(forKey: "product_dtls") as AnyObject) as! NSArray
            self.vehicleStatusStringArray = ((offlineResponseDict["server_productresponse"] as AnyObject) .value(forKey: "veh_status") as AnyObject) as! NSArray
      }
      self.homeTableView.reloadData()
 }

2 个答案:

答案 0 :(得分:0)

为什么在主线程上设置变量?这是不必要的,可能会锁定您的视图,这可能会给出禁用用户交互的外观。在主线程上做任何与UI无关的事情都应立即更改。应该在主线程上的代码的唯一部分是重载函数。

答案 1 :(得分:0)

试试这个:

func homePageAPI() {
    let kUserDefault = UserDefaults.standard

    DispatchQueue.global(qos: .background).async {
        // do something that needs to be run on background thread
        if Reachability.isConnectedToNetwork() {
            APIModel().homeApi(withUrl: "", withParameters:"", withComplitionBlock: {
                (_ responseDict: Dictionary!, _ error:String) -> Void in// here im not displaying my url as it is illegal
                if (responseDict != nil)
                {
                    kUserDefault.set(responseDict, forKey: "responseDict")
                    self.productresponseArray = (responseDict["server_productresponse"] as AnyObject) as! NSArray
                    self.productDetailsArray = ((responseDict["server_productresponse"] as AnyObject) .value(forKey: "product_dtls") as AnyObject) as! NSArray
                    self.vehicleStatusStringArray = ((responseDict["server_productresponse"] as AnyObject) .value(forKey: "veh_status") as AnyObject) as! NSArray
                }

            })

        } else{
            let offlineResponseDict:NSDictionary = kUserDefault.value(forKey: "responseDict") as! NSDictionary
            print(offlineResponseDict as Any)
            self.productresponseArray = (offlineResponseDict["server_productresponse"] as AnyObject) as! NSArray
            self.productDetailsArray = ((offlineResponseDict["server_productresponse"] as AnyObject) .value(forKey: "product_dtls") as AnyObject) as! NSArray
            self.vehicleStatusStringArray = ((offlineResponseDict["server_productresponse"] as AnyObject) .value(forKey: "veh_status") as AnyObject) as! NSArray
        }
        // update UI on main thread
        DispatchQueue.main.async {
            self.homeTableView.reloadData()
        }

    }
}