In Swift, why GCD is not working with parse?

∥☆過路亽.° 提交于 2019-12-13 03:41:19

问题


I have already asked this on In Swift, how to stop all the process until datas retrieved from parse.com in UICOLLECTIONVIEW. I couldn't retrieve the datas from parse.com before next function get executed. I don't know how to access asynchronous thread. I have declared main queue to be "first_fun()", so this should be run first. Likewise, it is running first, but ending at last. Before that, next function ("second_fun()") gets executed. How to QUEUE this function block? How to finish asynchronous thread first? Kindly check my code.

MY CODE IS BELOW:

override func viewDidLoad() {

println("START")
let queue : dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
   dispatch_async(dispatch_get_main_queue(), { () -> Void in 
   self.first_fun()
  })
})

second_fun()
println("END")

}

//FIRST_FUN

func first_fun() {
println("FIRST CLASS TOP")
self.par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in

if (error != nil) {
     NSLog("error " + error.localizedDescription)
}
else {

println("WELCOME to PARSE")           

}//ELSE ENDING

})//PARSE ENDING
println("FIRST CLASS BOTTOM")

}

//SECOND_FUN

func second_fun() {

println("WELCOME to SECOND")

}

回答1:


What you could do is add a callback to the first_fun and call second_fun inside that callback like so:

func first_fun(callback: () -> ()) {
   //do something async e.g your call to parse

    self.par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in
        if (error != nil) {
            NSLog("error " + error.localizedDescription)
        }
        else {
            println("WELCOME to PARSE")           

        }//ELSE ENDING
        callback()
    })//PARSE ENDING
}

Your viewDidLoad would look like this:

override func viewDidLoad() {
    let queue : dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    dispatch_async(queue, {
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.first_fun() {
                self.second_fun()
            }
        })
    })
}

You should of course also parameterize that callback to have access to the data or error from parse

For reference: Further info for completion blocks in swift




回答2:


The essence of your question is "how do I turn an asynchronous design into a synchronous one," which doesn't make a lot of sense. People hit this wall when they are trained in traditional procedural-style programming and then try to solve problems in a functional/event based system.

The answer to your question is "don't do that." You have to learn a new style of system design where everything that happens in second_fun() is not dependent on the results of first_fun(). If the first and second are truly dependent then you should be calling second_fun() as the last operation in first_fun().

For example, if you have a view that depends on data you pull down from the internet (which can be a long running operation), you would typically setup your view to show a spinning wait-indicator, and then you would make your call to findObjectsInBackgroundWithBlock(). In the callback you would process the found results, initialize other UI elements, and then replace the waiting-indicator with your desired view contents.

You have to stop thinking procedurally and start thinking functionally.



来源:https://stackoverflow.com/questions/28249105/in-swift-why-gcd-is-not-working-with-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!