Skip to content

Commit

Permalink
Merge pull request #10 from BrainCrumbz/feat/loop-completion-post-lock
Browse files Browse the repository at this point in the history
Feat: notify loop completion out of locked section to avoid client deadlocks
  • Loading branch information
rushairer authored Dec 5, 2021
2 parents 31be0aa + b54a1e7 commit 08b38f7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion YYImage/YYAnimatedImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ - (void)step:(CADisplayLink *)link {
delay = [image animatedImageDurationAtIndex:nextIndex];
if (_time > delay) _time = delay; // do not jump over frame
}
BOOL shouldNotifyLoopCompletion = NO;
NSUInteger remainingLoopNr = 0;
LOCK(
bufferedImage = buffer[@(nextIndex)];
if (bufferedImage) {
Expand All @@ -498,7 +500,8 @@ - (void)step:(CADisplayLink *)link {
_curIndex = nextIndex;
[self didChangeValueForKey:@"currentAnimatedImageIndex"];
if (_curIndex + 1 == _totalFrameCount && self.loopCompletionBlock) {
self.loopCompletionBlock(_totalLoop - _curLoop);
shouldNotifyLoopCompletion = YES;
remainingLoopNr = _totalLoop - _curLoop;
}
_curFrame = bufferedImage == (id)[NSNull null] ? nil : bufferedImage;
if (_curImageHasContentsRect) {
Expand All @@ -515,6 +518,10 @@ - (void)step:(CADisplayLink *)link {
}
)//LOCK

if (shouldNotifyLoopCompletion) {
self.loopCompletionBlock(remainingLoopNr);
}

if (!_bufferMiss) {
[self.layer setNeedsDisplay]; // let system call `displayLayer:` before runloop sleep
}
Expand Down

0 comments on commit 08b38f7

Please sign in to comment.