0

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.

Robert Brax
  • 6,508
  • 12
  • 40
  • 69
  • 1
    possible duplicate of [Inout parameter in async callback does not work as expected](http://stackoverflow.com/questions/28246949/inout-parameter-in-async-callback-does-not-work-as-expected) – rintaro Feb 23 '15 at 14:19
  • Why you see `"is ready"` once without `backgroundImageIsReady = false`? That's because `backgroundImageIsReady` is immediately reassigned with *unmodified* value when `initial()` returns. Check it out with `println(oldValue)` in `didSet` observer. – rintaro Feb 23 '15 at 14:23
  • ok got it thanks. Two problem here: first as pointed in the post, it's a closure problem with only immediately returned value passed to the inout parameter, not waiting for the async to do what it's supposed to. I used a struct instead of an inout. Second problem was my code MVC structure: I should have not put in a class file code responsible for changing views and leave that to the controller... otherwise it creates lots of problem. – Robert Brax Feb 23 '15 at 15:21

0 Answers0