multithreading - Facebook API Threading in Swift -
one think executing fbsdkgraphrequestconnection
, processing result straightforward task, seems threading causing problems.
i trying load facebook friends list , display in table. pretty basic. retrieving list no problem, table tries created before list has been retrieved. threading issue--the table create starts before list retrieval finishes. i've tried several different dispatchgroup
suggestions, seems fbsdkgraphrequestconnection
runs independently dispatchgroup
(and seems running on main thread, too).
i have gotten several other sample programs work properly, none use facebook api, leads me believe that block queued independently. i'm looking suggestions how block tableview(_:cellforrowat:)
until after fbsdkgraphrequstconnection.connection
finished.
for reference, here's code retrieve friend's list (again, part working fine).
let params = ["fields": "id, first_name, last_name, name, email, picture"] let graphrequest = fbsdkgraphrequest(graphpath: "/me/friends", parameters: params) let connection = fbsdkgraphrequestconnection() connection.add(graphrequest, completionhandler: { (connection, result, error) in if error == nil { print("request friends result : \(result!)") } else { print("error getting friends \(string(describing: error))"); } }) connection.start()
edit: requested, here's 1 of more promising attempts @ getting work. comes https://www.raywenderlich.com/148515/grand-central-dispatch-tutorial-swift-3-part-2. in exercise, 3 pictures downloaded internet, alert box indicating pictures have finished downloading appears before they've finished downloading. first snippet handles request download images internet , displays alert:
// internet button let internetaction = uialertaction(title: "le internet", style: .default) { _ in self.downloadimageassets() } alert.addaction(internetaction)
the images downloaded using:
func downloadimageassets() { photomanager.sharedmanager.downloadphotoswithcompletion() { error in let message = error?.localizeddescription ?? "the images have finished downloading" let alert = uialertcontroller(title: "download complete", message: message, preferredstyle: .alert) alert.addaction(uialertaction(title: "ok", style: .cancel, handler: nil)) self.present(alert, animated: true, completion: nil) } }
the original downloadphotoswithcompletion(_:) returns immediately, 2 different fixes given. first (and more explicit more cumbersome), both work equivalently:
func downloadphotoswithcompletion(_ completion: batchphotodownloadingcompletionclosure?) { dispatchqueue.global(qos: .userinitiated).async { var storederror: nserror? let downloadgroup = dispatchgroup() address in [overlyattachedgirlfriendurlstring, successkidurlstring, lotsoffacesurlstring] { let url = url(string: address) downloadgroup.enter() let photo = downloadphoto(url: url!) { _, error in if error != nil { storederror = error } downloadgroup.leave() } photomanager.sharedmanager.addphoto(photo) } downloadgroup.wait() dispatchqueue.main.async { completion?(storederror) } }
i, then, tried replacing loop facebook call:
dispatchqueue.global(qos: .userinitiated).async { let downloadgroup = dispatchgroup() let params = ["fields": "id, first_name, last_name, name, email, picture"] let graphrequest = fbsdkgraphrequest(graphpath: "/me/friends", parameters: params) let connection = fbsdkgraphrequestconnection() connection.add(graphrequest, completionhandler: { (connection, result, error) in if error == nil { print("request friends result : \(result!)") } else { print("error getting friends \(string(describing: error))"); } downloadgroup.leave() }) connection.start() downloadgroup.wait() dispatchqueue.main.async { completion?(nil) } }
the behavior same--the table starts being created before facebook call completes.
Comments
Post a Comment