I have a bool variable in a view controller, that is supposed to be updated when its value is changed from a class file.
View controller:
var backgroundImageIsReady: Bool = true {
didSet {
if backgroundImageIsReady {
activityIndicator.stopAnimating()
println("is ready")
} else {
println("is not ready")
}
}
}
View controller function invocation (yes I know it's fat):
self.imageLoad.initial(self.imageResourcesArray,
container2: self.imageContainer2,
container3: self.imageContainer3,
ifUserIsNew: ifNewUser,
backgroundImageIsReady: &backgroundImageIsReady)
Class function definition:
class ImageLoad {
// MARK: Load initial 2 images
func initial(imageArray: [PFFile], container2: UIImageView, container3: UIImageView, ifUserIsNew: () -> (), inout backgroundImageIsReady: Bool ) {
backgroundImageIsReady = false // here
imageArray[0].getDataInBackgroundWithBlock{
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
let image = UIImage(data: imageData)
container3.image = image
container3.alpha = 0
backgroundImageIsReady = true // Here
UIView.animateWithDuration(0.5, animations: {
container3.alpha = 1
ifUserIsNew()
})
}
}
}
You can see in the function definition that as soon as it is invoked, it should set the backgroundImageIsReady from the view controller to false, then when image has finished downloaded, set it to true. What happens in this example, is that the variable is indeed set to FALSE, the set to TRUE are ignored and println() will print "is not ready" only. When I remove the set to false statement backgroundImageIsReady = false than I see printed "is ready" once. It's an incoherent behavior I can't understand. It seems the variable observer will only do it's job once only when it's first changed, then ignore the other changes.