Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Commit bea213a

Browse files
andrewovertonmaterial-automation
authored andcommitted
Fix SnackBar crash when snackbarView is nil
These changes ensure that the CABasicAnimations involved in sliding snack bars on and off the screen are non-nil before they're added to an array. I tested it by commenting out the following snackbarView-related code here to ensure that the snackbarView would be nil: https://github.com/material-components/material-components- ios/blob/develop/components/Snackbar/src/MDCSnackbarManager.m#L209-L230) Without the changes in this CL it crashed, which was expected. With the changes, it did not crash. However, the snack bar did not show. Future changes should provide better error handling to help elucidate the circumstances in which the snackbarView. Improved error handling might entail adding NSError parameters to the completion blocks, for example. In the immediate term, however, my feeling is that we should just fix the crash. PiperOrigin-RevId: 302490569
1 parent 9a006b8 commit bea213a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

components/Snackbar/src/private/MDCSnackbarOverlayView.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,18 @@ - (void)slideMessageView:(MDCSnackbarMessageView *)snackbarView
580580
duration:duration
581581
timingFunction:timingFunction];
582582
} else {
583-
NSMutableArray *animations =
584-
[NSMutableArray arrayWithObject:[snackbarView animateSnackbarOpacityFrom:fromContentOpacity
585-
to:toContentOpacity]];
583+
NSMutableArray *animations = [[NSMutableArray alloc] init];
584+
CABasicAnimation *opacityAnimation = [snackbarView animateSnackbarOpacityFrom:fromContentOpacity
585+
to:toContentOpacity];
586+
if (opacityAnimation) {
587+
[animations addObject:opacityAnimation];
588+
}
586589
if (onscreen) {
587-
[animations addObject:[snackbarView animateSnackbarScaleFrom:MDCSnackbarEnterStartingScale
588-
toScale:1]];
590+
CABasicAnimation *scaleAnimation =
591+
[snackbarView animateSnackbarScaleFrom:MDCSnackbarEnterStartingScale toScale:1];
592+
if (scaleAnimation) {
593+
[animations addObject:scaleAnimation];
594+
}
589595
}
590596
animationsGroup.animations = animations;
591597
[snackbarView.layer addAnimation:animationsGroup forKey:@"snackbarAnimation"];

0 commit comments

Comments
 (0)