1

I am trying to figure out why swift is returning the 0 that I declared in the beginning of the function instead of returning the number that I set it to from Parse.

func getPhotosSubmitted(quest: String) -> Int{
   var num = 0
   var query = PFQuery(className:"Quest")

    query.whereKey("questName", equalTo:quest)

    query.findObjectsInBackgroundWithBlock {

        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {

            // The find succeeded.

          //  NSLog("Successfully retrieved \(objects.count) scores.")

            // Do something with the found objects

            for object in objects {

               num = (object["numOfSubmittedPhotos"] as Int)

                println("---\(num)")
            }
        } else {
            // Log details of the failure
            //NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }

   return num
}

This func will return 0 everytime, as if it is not waiting for: num = (object["numOfSubmittedPhotos"] as Int) When I try to return (object["numOfSubmittedPhotos"] as Int) it throws an error saying that Int is not convertible to void I print a test line to see what value I get from Parse and it is the correct value that is not 0. Does anyone see a problem in this code?

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
realroo
  • 423
  • 6
  • 9
  • the question title already suggest the problem: the function return _before_ parse can get a value. you have to redesign it – Bryan Chen Oct 28 '14 at 02:16
  • How would I do so? I felt that it would wait until it got the value and then inserted it into the variable num before returning as in most languages. – realroo Oct 28 '14 at 02:29
  • `findObjectsInBackgroundWithBlock` does this method name tell you soomething? you need to learn how do to asynchronous programming – Bryan Chen Oct 28 '14 at 02:30
  • Okay. Well hopefully you can point me in the right direction instead of demoralizing me? I am posting here because I obviously do not comprehend it. – realroo Oct 28 '14 at 03:09
  • it is for ObjC, but idea is same http://stackoverflow.com/questions/17642535/return-value-for-function-inside-a-block – Bryan Chen Oct 28 '14 at 03:11
  • Unfortunately I havent picked up ObjC yet! But thank you, I will try to figure out what they are doing in ObjC and implement it in Swift – realroo Oct 28 '14 at 03:52
  • Another option, albeit more complex, but i think ultimately simpler, would be to also study the Bolts Framework and/or ReactiveCocoa.. both provide pathways to overcoming some of the baked in difficulty with async processes. – drew.. Oct 30 '14 at 14:54

1 Answers1

0

query.findObjects... creates a block that will EVENTUALLY run. Right after that, you return num. That happens NOW.

Since getNumberOfPhotos wants to return a value that will only be available in the future, it too must be an asynchronous call. That means the return must be to a block to be executed in the future or something like that.

As someone said, spend a day reading up an asynchrony in iOS, concentrate on blocks as opposed to the other methods.

LostInTheTrees
  • 1,135
  • 9
  • 19