Necessary to unsubscribe from sensors on app exit?

LeloLelo Member ✭✭✭

I am currently experiencing an illusive crash which occurs when the app is quickly restarted. Since the app uses a lot of threads related to the use of sensors - I suspect it might be necessary to handle unsubscribe - and stopping of the sensor services in the app exit event?
Should I unsubscribe and stop sensor services in my Xamarin forms app?

Best Answer

  • LeloLelo ✭✭✭
    edited December 2019 Accepted Answer

    Thank you for your suggestion.~~~~

    It was Device.BeginInvokenMainThread this time though. I think my app is very demanding on processeror - so all event handler code gets execute on another thread. I had missed one place:

    SearchViewModel.Get().OnSearched = () =>
        {
            var targets = TargetViewModel.Get().Targets;
            Device.BeginInvokeOnMainThread(() =>
            {
                foreach(var visual in visualItems)
                 {
                        content.Children.Remove(visual);
                 }
                 visualItems.Clear();
                 foreach (var target in targets)
                 {
                    visualItems.Add(new VisualItem(target));
                 }
              });
           };
    

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, try to remove these services and test again to confirm this is caused by the sensor services.
    Secondly, I think it's hard to detect when the application will be closed as it is controlled by the user. However, we can detect whether these services are still connected when the application starts. Then either skip the building connection process or disconnect them if the connection existed.

  • LeloLelo Member ✭✭✭
    edited December 2019 Accepted Answer

    Thank you for your suggestion.~~~~

    It was Device.BeginInvokenMainThread this time though. I think my app is very demanding on processeror - so all event handler code gets execute on another thread. I had missed one place:

    SearchViewModel.Get().OnSearched = () =>
        {
            var targets = TargetViewModel.Get().Targets;
            Device.BeginInvokeOnMainThread(() =>
            {
                foreach(var visual in visualItems)
                 {
                        content.Children.Remove(visual);
                 }
                 visualItems.Clear();
                 foreach (var target in targets)
                 {
                    visualItems.Add(new VisualItem(target));
                 }
              });
           };
    
Sign In or Register to comment.