把swift的代码中的http请求换成异步的:
func getUrlRespJsonDict_async(url:String, extraParas:AnyObject?, respJsonDictHandler:((Bool, AnyObject?, JSON) -> Void)) { print("url=\(url)") var getJsonOk:Bool = false let params:Dictionary<String, AnyObject>? = nil do { let opt = try HTTP.GET( url, parameters: params, requestSerializer: JSONParameterSerializer()) opt.start { response in if let err = response.error { print("url=\(url) with params=\(params) response error= \(err.localizedDescription)") getJsonOk = false let errJsonDict = JSON(["error" : err.localizedDescription]) respJsonDictHandler(getJsonOk, extraParas, errJsonDict) return } //print("response.statusCode=\(response.statusCode)") //print("opt finished: \(response.description)") let decodedJsonDict:JSON = JSON(data: response.data) print("decodedJsonDict=\(decodedJsonDict)") getJsonOk = true respJsonDictHandler(getJsonOk, extraParas, decodedJsonDict) } } catch let httpGetError { print("url=\(url) with params=\(params) got error= \(httpGetError)") getJsonOk = false let errJsonDict = JSON(["error" : "\(httpGetError)"]) respJsonDictHandler(getJsonOk, extraParas, errJsonDict) } } func contactIdListJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON){ if getJsonOk { var contactIdList:[String] = [String]() contactIdList = respJsonDict.arrayObject as! [String] print("contactIdList=\(contactIdList)") // let userTeamInfoDict:[String:String] = extraParas as! [String:String] let userIdStr:String = userTeamInfoDict["userIdStr"]! as String let teamIdStr:String = userTeamInfoDict["teamIdStr"]! as String let totalContactNum:Int = contactIdList.count for eachContactIdStr in contactIdList { let getContactInfoUrl:String = StrServerUrl + "/user/" + userIdStr + "/teams/joined/" + teamIdStr + "/members/" + eachContactIdStr print("getContactInfoUrl=\(getContactInfoUrl)") //http://192.168.1.110:8080/user/user-7aa7e3c3-bf8f-46e1-8cbe-3a5aad6e9dfb/teams/joined/team-b6c09caa-4e08-48d7-a415-ef1f69f8c588/members/user-22719dc9-7f4b-47ab-807d-9b2146b1344a getUrlRespJsonDict_async(getContactInfoUrl, extraParas: totalContactNum, respJsonDictHandler: singleContactItemJsonDictHandler) } } } func singleContactItemJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON?){ if getJsonOk { var singleContactItem:ContactItem = ContactItem() singleContactItem.nameStr = respJsonDict!["name"].string! //singleContactItem.phoneNumberStr = respJsonDict!["phone"].stringValue print("singleContactItem=\(singleContactItem)") insertContactItemToList(singleContactItem) //TODO: reload tableview data for each contact item ? let totalContactNum:Int = extraParas as! Int if getCurTotalContactItemNum() >= totalContactNum { //reload contact tableView print("gContactViewController=\(gContactViewController)") print("gContactViewController.contactTableView=\(gContactViewController!.contactTableView)") //gContactViewController?.contactTableView.reloadData() gContactViewController!.contactTableView.reloadData() } } }
之后,经常遇到如下问题:
希望是
调用了:
gContactViewController!.contactTableView.reloadData()
后,联系人列表,会刷新为:
但是
此处,经常遇到:
reloadData没生效,而联系人列表是旧的内容:
而没有刷新。
搜:
swift tableview reloaddata sometime not work
swift async tableview reloaddata sometime not work
参考:
去试试:
dispatch_async(dispatch_get_main_queue(), { () -> Void in //gContactViewController?.contactTableView.reloadData() gContactViewController!.contactTableView.reloadData() })
然后真的可以了,正常,立刻,及时的刷新数据,页面上的联系人保持最新的值。
[总结]