please me solve problem - after lot of (not efficent...) search can't alone.
i have following methods:
showloadinganimation() to show loading animation while background tasks running
hideloadinganimation() to hide loading animation background tasks finished
getuserfacebookdata() to facebook-user data
uploaduserfacebookdatatoserver() to upload facebook-user data server (and perform tasks them).
what want perform:
- show loading animation: showloadinganimation()
- get user data facebook: getfacebookdata()
- wait until these data being downloaded
- as facebook-user data being download, upload these data server: uploaduserfacebookdatatoserver()
- wait untile these data being uploaded
- hide loading animation: hideloadinganimation()
now problem is, don't know how solve problem. know, should use sync and/or async tasks, gcd... don't know how, , can't find proper guide it.
could explain me through these functions? thanks!
update:
thank you, zhi-wei cai, kind of answer hoping for.
now seems work, calling order ok, problem same beginning:
uploaduserfacebookdatatoserver() doesn't wait until
getuserfacebookdata downloads user data facebook, that's why won't able work necessary data given from
getuserfacebookdata any idea? there dispatch?
update 2:
as requested, here fuctions. hope, these information can me solve problem , understand whole process.
func getfacebookuserdata(completionhandler: () -> void) { println("getfacebookuserdata") let graphrequest : fbsdkgraphrequest = fbsdkgraphrequest(graphpath: "me", parameters: nil) graphrequest.startwithcompletionhandler({ (connection, result, error) -> void in if ((error) != nil) { // process error println("error: \(error)") } else { let userid : nsstring = result.valueforkey("id") nsstring! var defaults: nsuserdefaults = nsuserdefaults.standarduserdefaults() defaults.setobject(userid, forkey: "settings_facebookid") self.facebookid_fromsettings = userid } }) and
func getobjectidfromfacebookid(completionhandler: () -> void) { println("getobjectidfromfacebookid") var query = pfquery(classname:"users") query.wherekey("facebookid", equalto:facebookid_fromsettings) println("getobjectidfromfacebookid: facebookid: " + facebookid_fromsettings) query.findobjectsinbackgroundwithblock { (objects: [anyobject]?, error: nserror?) -> void in if error == nil { // find succeeded. println("successfully retrieved \(objects!.count) scores.") // found objects if (objects!.count == 0) { // new user, registering println("new user, registering") } else { //user regsitered, reading out objectid println("user regsitered, reading out objectid") } if let objects = objects as? [pfobject] { object in objects { println("objectid: " + object.objectid) var objectid: string = object.objectid println(objectid) var defaults: nsuserdefaults = nsuserdefaults.standarduserdefaults() defaults.setobject(objectid, forkey: "settings_objectid") } } } } completionhandler() } so first function gets facebookid fb-server, process takes time, won't give result immediately. second function should work data, that's why should "wait" until first gives requested data.
i can solve problem building these 2 fuctions in one, that's "not elegant", , use (sync/async dispatch) method in other parts of project,
thanks trying me!
you can use completion handlers:
func showloadinganimation() { self.getuserfacebookdata({ () -> void in self.uploaduserfacebookdatatoserver({ () -> void in self.hideloadinganimation() }) }) } func getuserfacebookdata(completionhandler: () -> void) { println("getuserfacebookdata") completionhandler() } func uploaduserfacebookdatatoserver(completionhandler: () -> void) { println("uploaduserfacebookdatatoserver") completionhandler() } func hideloadinganimation() { println("hideloadinganimation") } once showloadinganimation() called, rest done asynchronously.
reference: https://developer.apple.com/library/ios/featuredarticles/short_practical_guide_blocks/
Comments
Post a Comment