json - iOS Swift 3 CompletionHandler not working -


i'm trying write completion handler function in swift, here's code in swift class called nbobject

typealias completionhandler = (_ success: bool, _ data: [string:any]) -> void  // find objects in background func findobjectinbackground(completionhandler: completionhandler) {     let tbnamestr = "table=" + thetablename     var rstr = ""     var thedata = [string:any]()      in 0..<columns.count {         rstr += "&c" + "\(i)" + "=" + columns[i] +             "&r" + "\(i)" + "=" + records[i]     }     recordstring = tbnamestr + rstr     print("record string: \(recordstring)")      let requesturl = url(string: path_to_api_folder + "querywherekeycontains.php?" + recordstring)      //create session object     let session = urlsession.shared      //now create urlrequest object using url object     let request = urlrequest(url: requesturl!)      //create datatask using session object send data server     let task = session.datatask(with: request urlrequest, completionhandler: { data, response, error in          //exiting if there error         if error != nil {             print("error is: \(error!.localizeddescription)")             return         } else {          }          {             if let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? [string: any] {                 thedata = json                 print("json: \(json)")             }         } catch let error { print("error in parsing json: \(error.localizeddescription)") }      })      task.resume()       let flag = true     completionhandler(flag, thedata) } 

i call function in viewcontroller.swift, this:

let query = njobject(tablename: hotel_table_name) query.wherekeycontains(columnname: hotel_name, record: "hotel") query.findobjectinbackground { (succ, objects) in     print("objects: \(objects)")     print("success: \(succ)") } 

so, in xcode console, correctly json data, when printing completion handler, data (printed objects) empty.

objects: [:] success: true json: ["objects": <__nsarraym 0x17004b4c0>( {     address = "<null>";     createdat = "2017-08-12 23:08:48";     description = "lorem ipsec dolor sit";     email = "<null>"; }, {     address = "<null>";     createdat = "2017-08-14 06:19:10";     description = "lorem ipsec dolor sit";     email = "<null>"; }) ] 

one thing noticed console first prints objects , empty [:] , success logs, json data.

so sure there's wrong in findobjectinbackground() function, can't figure out issue is.

any appreciated!

you have put completion handler into completion handler of data task

you can omit creation of urlrequest, default, pass url:

let task = session.datatask(with: requesturl!, completionhandler: { data, response, error in      //exiting if there error     if error != nil {         print("error is: \(error!.localizeddescription)")         completionhandler(false, [:])         return     }       {         if let json = try jsonserialization.jsonobject(with: data!) as? [string: any] {             print("json: \(json)")             completionhandler(true, json) // variable `thedata` not needed.         }     } catch let error { print("error in parsing json: \(error.localizeddescription)") }       completionhandler(false, [:]) })  task.resume() 

and not pass .mutablecontainers, it's meaningless in swift

side note: in swift 3 closure declaration sufficient:

typealias completionhandler = (bool, [string:any]) -> void 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -