calimarkus / JDFlipNumberView

[iOS] Animated analog flip numbers like airport/train-station displays (Swift/SwiftUI ready)
MIT License
786 stars 138 forks source link

Nested completion blocks aren't working #30

Closed calimarkus closed 10 years ago

calimarkus commented 10 years ago

Discussion with https://github.com/MattFoley/JDFlipNumberView/commit/dcdbd013ea5574643d89ceed1ba1f935348e2a73

I'm sorry, this is a bit in progress on my fork and didn't have time to fully fix it. The starting issue, an infinite loop, was caused calling something like:

[self.countdownView animateToValue:someValue duration:someDuration  completion:^(BOOL finished) {
    [self.countdownView animateToValue:someValue duration:someDuration completion:^(BOOL finished) {
        if (finished) {
            [self doStuff];
        }
    }];
}];

The first line of animateToValue called [self stopAnimation] would try to call the previous completion block, which executed the second animateToValue, which would hit the [self stopAnimation] again, on and on forever.

You can see in my fork I started to try to fix it but wasn't able to fully fix it before I ran out of time.

My current workaround is this:

[self.countdownView animateToValue:someValue duration:someDuration  completion:^(BOOL finished) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.flipNumberView animateToValue:someValue duration:someDuration completion:^(BOOL finished) {
            if (finished) {
                [self doStuff];
            }
        }];
    });
}];

I believe the timer issue was just a nested block/threading issue as a result of some of the other changes I've made.

CC: @MattFoley

calimarkus commented 10 years ago

@MattFoley fixed it today!