swiftparse-platformnanpfobject

How to access a variable value outside of Parse's getObjectInBackgroundWithId block


using the following Swift code, I need to print value of 5/hhhh. First, hhhh is set to zero at "var hhhh:Int = 0", then, inside "getObjectInBackgroundWithId" block, the value of hhhh gets updated by "hhhh = appleCount["count"] as! Int" using the data that was just queried from Parse. Next, I need to somehow access the updated value of hhhh outside the "getObjectInBackgroundWithId" block and print the following value "print(5/hhhh)", but, value of hhhh is still zero on this line and I'm getting NAN doing this operation. osteven kindly gave me some hints on why value of hhhh is zero at this point here:

Local and Global variables in Swift

But, I haven't figured a way to somehow transfer the updated value of hhhh to outside of "getObjectInBackgroundWithId" block and use that value at "print(5/hhhh)" instead of zero. Any help or hints on how to make this happen are highly appreciated

import UIKit
import Parse
class NewsPageViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad(
var hhhh:Int = 0
var tttt:Int = 0
var cccc:Int = 1

if cccc == 1 {
    var query = PFQuery(className: "Count")
    query.getObjectInBackgroundWithId("RhC25gVjZm", block: { (object: PFObject?, error: NSError?) -> Void in
        if error != nil {
            print(error)
        } else if let appleCount = object {
            appleCount["count"] = appleCount["count"] as! Int + 1                
            hhhh = appleCount["count"] as! Int
            appleCount.saveInBackground()
        }
    })
} 
print(5/hhhh)

}


Solution

  • The answer you have already been given explains the issue well in my opinion, I will try to reword the issue to help you understand how to fix it.

    The getObjectInBackgroundWithId function is running a block at a different time to that of the currently running print(5/hhhh) statement.

    So the value 'hhhh' is being updated however you are trying to call on the hhhh value in the print statement before the block in getObjectInBackgroundWithId has had time to run and update.

    In other words,

    print(5/hhhh)
    

    is happening before this block of code is being executed (due to the nature of background threads),

    query.getObjectInBackgroundWithId("RhC25gVjZm", block: { (object: PFObject?, error: NSError?) -> Void in
        if error != nil {
            print(error)
        } else if let appleCount = object {
            appleCount["count"] = appleCount["count"] as! Int + 1                
            hhhh = appleCount["count"] as! Int
            appleCount.saveInBackground()
        }
    })
    

    A way of utilising the hhhh variable once you know it has been updated, would be to create a function to use (in your case print) the variable and call on it within the block, like so:

    EXAMPLE CODE:

    func doSomethingWithHHHH(){
      print(5/hhhh)
    }
    
    query.getObjectInBackgroundWithId("RhC25gVjZm", block: { (object: PFObject?, error: NSError?) -> Void in
            if error != nil {
                print(error)
            } else if let appleCount = object {
                appleCount["count"] = appleCount["count"] as! Int + 1                
                hhhh = appleCount["count"] as! Int
        //HERE YOU CALL ON THE FUNCTION TO USE THE VARIABLE
                doSomethingWithHHHH()
                appleCount.saveInBackground()
            }
        })
    

    In this example the print statement should work, as the variable has been updated in the block before the function is called to use the 'hhhh' variable.