locked
Best place to attach listeners for Firebase Database in Xamarin.Forms? RRS feed

  • Question

  • User328810 posted

    Hi guys! So firstly since there is not a lot of guidance with respect to setting up Xamarin.Firebase.Database in a Shared Project (or PCL) in Xamarin.Forms I will post my working setup here. There is documentation for Swift and Objective-C in X-Code but I couldn't find anything for C# and Xamarin.Forms. The setup is only for the iOS platform in Visual Studio but Android is similar.

    1. When creating the Firebase App in the Firebase console you have to use the same Bundle ID as your project's.
    2. The bundle ID for your App can by found in the Info.plist file in the iOS project.
    3. Download the Google Services .plist file as instructed in the Firebase App setup and add it to your platform's directory - for iOS it should be in [YourApp].iOS right above the Info.plist file.
    4. Set the .plist file's build method to BundleResource
    5. In AppDelegate.cs add using FIR = Firebase.Analytics; and right above the return statement in the launching method you configure the app: FIR.App.Configure()

    This concludes the setup and you should be able to use Firebase from shared code.

    So returning to my original question about where to attach the listeners:

    I have a page called SpecialsListPage that is my root page in a NavigationPage setup. In the constructor of SpecialsListPage I set the BindingContext of the page to a view model called SpecialsViewModel. The SpecialsViewModel class has a Specials property that is an ObservableCollection<Special> type that is set as the ItemSource for my ListView in the SpecialsListPage page.

    I tried to google what the best place would be to attach my event listeners for the database but I only found examples for Swift: The post said to place listeners in the viewWillAppear method and to remove listeners in viewDidDisappear method. Now these two methods are similar to OnAppearing and OnDisappearing in the Page class.

    So what I did was to override these two methods in the SpecialsListPage class like this:

                    protected override void OnAppearing()
                    {
                        // call function to attach listeners for child added event type
                        // callback for snapshot has access to the list of Specials that is the ItemSource of the ListView
                    }
    
                    protected override void OnDisappearing()
                    {
                        // remove listeners using handle
                    }
    

    Now this works just fine when the app starts up. But when the user clicks on a Special that takes them to the next page in the NavigationPage setup and then clicks back to go to the SpecialsListPage, the OnAppearing method is called again and attaches a new listener to my node and that causes the list to update again with the same data thus causing duplicates. So is this the best place to place the listeners or am I doing this wrong? Any help would be greatly appreciated :)

    Saturday, June 24, 2017 1:52 PM

Answers

  • User113114 posted

    Subscribe in ctor, unsub when page is popped by using the Popped() event on NavigationPage. Dont trust onappearing/dissapearing

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, June 24, 2017 10:28 PM