galadril/Xam.Shell.Badge

Crashes on App Start on iOS

Closed this issue · 10 comments

I am seeing the following crashes when using the PlugIn on iOS phones (15.5):

System.Exception: Caught iOS Unhandled Exception from MonoTouch: System.NullReferenceException: Object reference not set to an instance of an object.
  at Xam.Shell.Badge.iOS.Renderers.BadgeShellItemRenderer.InitBadges () <0x1164c9318 + 0x0009a> in <f4de591364874ce18d007b3fc2f93d3e#f0cba61e4def1f184688af012be96473>:0 
  at (wrapper delegate-invoke) <Module>.invoke_void()
  at Xamarin.Forms.Device+<>c__DisplayClass43_0.<InvokeOnMainThreadAsync>g__wrapAction|0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Device.cs:130 
  at (wrapper delegate-invoke) System.Func`1[System.Object].invoke_TResult()
  at Xamarin.Forms.Device+<>c__DisplayClass42_0`1[T].<InvokeOnMainThreadAsync>b__0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Device.cs:116 
--- End of stack trace from previous location where exception was thrown ---

These happen as the application is loading and brings down the whole thing. I am seeing this on both the latest XF (5.0.0.2478) and the one with which this pkg was last released (version 1.20 -> XF 5.0.0.2401).

As you can see from the stack trace the app crashes on Xam.Shell.Badge.iOS.Renderers.BadgeShellItemRenderer.InitBadges().

The funny thing is that when we first load the app on a phone (fresh side-load or install) we see no crashes. But over time (in a few days on average) we start seeing these crashes - the work around if for users to remove the app and re-install it.

We were able to resolve the issue by adding null checks to the InitBadges() method on iOS. After that the method looks like this:

        private void InitBadges()
        {
            _tabRealIndexByItemId.Clear();
            if (ShellItem?.Items == null)
                return;
            for (int index = 0, filteredIndex = 0; index < ShellItem.Items.Count; index++)
            {
                var item = ShellItem.Items.ElementAtOrDefault(index);
                if (item == null || !item.IsVisible)
                    continue;
                _tabRealIndexByItemId[item.Id] = filteredIndex;
                UpdateBadge(item, filteredIndex);
                filteredIndex++;
            }
        }

Let me know if you'd like me to submit a PR.

Hi @Westat-Transportation,
I am facing the same problem and would like to try your solution. Did you download the source code or is there a way to override or extend the iOs Renderer to use your function?

@borrmann, we ended up forking the code and building it using our internal CI/CD and Westat's private nuget repo. Here's the link to the built library if you want to try that on your code base -> https://1drv.ms/u/s!Aqc5oOA9-xpJg8cF8CtWo31qF3S0Kg?e=mqWUsz

I would gladly submit a PR but it seems like the owner of this project is not monitoring its issues.

@galadril, just saw you gave my initial comment here a thumbs up. Should I submit a PR with the fix I described here?

Plz make a PR, ill publish then a new version

@galadril -> PR submitted.

New version is uploaded to nuget, pls close this issue if this solves it.

I am not running into any crashes anymore. So I think this should be marked closed! Thanks @Westat-Transportation and @galadril