iOS Swift code freezes with NSURL when sent from foreground to background to foreground again with 100% loss connection -
i have been developing swift 6 months new user. app i'm working on has focus on poor connections , slow speeds. app freezes under following conditions.
i have json query data server, uses nsurl / nsdata. if have connection works, if have no connection fails expected , report error user there no connection. but:
if connection has 100% loss (you can simulate via developer settings within phone, network link conditioner) query runs , app shows activity indicator. press home button put app background , click app icon open again. activity screen still displayed display frozen. activity indicator doesn't spin , battery icon , clock update draw on top of each other (there no refresh whatsoever).
after 30 seconds or app terminates. expect watchdog killing app due freeze.
if run same process phone connected xcode app doesn't terminate. assume watchdog doesn't kill tasks when running via xcode.
this may seen rare occurrence try catch bug / freeze in code cannot errors displayed hard do.
has seen before? know cause?
edit fails sending or receiving data via json, sending code simpler here code:
func sendseverv2(theurl: string, parameters: string) -> string { // build url request var status = "ok" let parameters2 = parameters.stringbyaddingpercentencodingwithallowedcharacters(.urlhostallowedcharacterset())! let urlwithparams = "\(theurl)?\(parameters2)" let myurl = nsurl(string: urlwithparams) let data = nsdata(contentsofurl: myurl!) guard data != nil else { return "error" } { let object = try nsjsonserialization.jsonobjectwithdata(data!, options: .allowfragments) if let jsondata = object as? [string: anyobject] { status = jsondata["status"] as! string } } catch { // report error } if status == "error" { print("🔴 json fetch had error \(urlwithparams)") } return status }
edit: function used many calls server. run on non main thread. there cases when have on main thread. reason (which may due inexperience) may have settings screen user can opt-in or opt-out of email alerts. need ensure user knows the settings have been applied. not user opt-out of email alert server call fail user thinks worked. in these cases user may have wait few seconds on slow connection or wait timeout on 100% loss connection app can display, "connection error".
Comments
Post a Comment