-
Notifications
You must be signed in to change notification settings - Fork 459
Fix NullReferenceException when displaying Toast or SnackBar #963
Conversation
@VladislavAntonyuk, @AndreiMisiukevich, @pictos Could you please take a look at this? Is there a better fix for this issue? |
It looks good in terms of code stability, but we do not show the Snackbar. |
@VladislavAntonyuk The thing is that the page can be "parent" for the Snackbar. This code fails only if we try to show Snackbar at the same time when we change current page. Both pages are fully capable of displaying Snackbar, but when we are in the middle of transitioning between them, And throwing exception here would mean that every call to |
As described in the issue, you run the Snackbar on page appearing. |
@maxkoshevoi Is this just occur with Shell? |
@VladislavAntonyuk Didn't try to reproduce it on other platforms. My app can be built only for Android as of now. I don't have time to create a new project and test it on UWP (don't have Mac to test it on iOS) right now.
Updated PR with retry logic instead of just returning. Tested and it works with 50 ms retry.
Well, in my use case,
@pictos I'm not sure about that. What I can say is that it definitely happens in
|
@maxkoshevoi because of that I said to wait until we see the issue and discuss it first. Since this only happens on Shell (did you test on TabbedPage?) and in a very specific moment, navigation I would that's a Shell problem and not our lib. Also, let's move this discussion to the issue and please can you provide a repro? |
src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/SnackBar.android.cs
Outdated
Show resolved
Hide resolved
@maxkoshevoi can you solve these conflicts? |
@pictos @brminnick Why is this retargeted to |
@maxkoshevoi I believe that happens because of the nullable PR, at that time we moved all PRs to target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
// COMMENT EXPLAINING THE PURPOSE OF THIS METHOD
static async Task<IVisualElementRenderer?> GetRendererWithRetries(Page page, int retryCount = 5)
{
var renderer = Platform.GetRenderer(page);
if (renderer != null || retryCount <= 0)
return renderer;
await Task.Delay(50);
return await GetRendererWithRetries(page, retryCount - 1);
}
As for Show
method.
Can we preserve Return-Type to be void
?
Maybe we can put the code after GetRendererWithRetries call to ContinueWith?
internal void Show(Page page, SnackBarOptions arguments) {
GetRendererWithRetries(sender).ContinueWith(....);
}
Any thoughts?
Good point. I'll add it
@AndreiMisiukevich Can I ask why do you want to preserve it as There are two options to preserve
|
@AndreiMisiukevich Can you explain why you need to preserve the @maxkoshevoi No, do not ever use If we do need to preserve the I recommend using |
Yeah, it's bad to use them since they block the thread. But that's the only way to preserve
@brminnick It won't help here since
Totally agree. I also don't see a reason not to do that |
I thought it is related to public API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, let's add a comment explaining what this method does, and then we will be able to get it merged, I believe. If there are no other objections.
And if you wish you can consider the "recursion-style" implementation of this method that I proposed above (up to you).
Thank you!
@AndreiMisiukevich Added comment and changed implementation to recursive one. |
@pictos can you please take a look again? |
src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/SnackBar.gtk.cs
Outdated
Show resolved
Hide resolved
src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/SnackBar.ios.macos.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just this little change
src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/SnackBar.android.cs
Outdated
Show resolved
Hide resolved
@maxkoshevoi thanks for the changes, I'll take a look today night or tomorrow during the day. |
@pictos Just a friendly reminder that PR is ready for merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxkoshevoi we have a TestPage folder inside the sample app, do you think that is possible to add a scenario there that proves that this PR fixes the issue? That isn't required to get this merged
@pictos It should be possible, but complicated. We would need to create mini-Shell environment with two pages inside our sample app |
In that case let ignore it for now. Thanks again for this PR |
Description of Change
Added retry logic for Android for getting renderer.
Bugs Fixed
API Changes
None
Behavioral Changes
None
PR Checklist