Vedasri Sai roja yerra Vedasri Sai roja yerra -4 years ago 89
Swift Question

user interaction was disabled when using offline storage

i have stored my json response in NSUserdefaults and i have to show that stored response even the data is in offline the result is ok
but user interaction for ui is disabled when offline.
hope can any one help

i have tried by giving break points at network reachability is connected or not it is going in to the loop and displaying response but ui interaction is strucked

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()
}

Answer Source

Why are variables set on the main thread? That is unnecessary and could be locking up your view, which may give the appearance that the userinteraction is disabled. Doing any thing on the main Thread that isn't related to UI, should be changed immediately. The only part of your code that should be on the main thread, is the reload function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download