weird error inserting into firebase

Eduardo Gomez 3,426 Reputation points
2022-02-03T00:36:21.013+00:00

Hello

I have an error, then I don't know what is going on

I am reading the database every time I insert a notebook, and I can see it, but if I go back and insert it, I cannot see it

https://reccloud.com/u/py7r1ob

project

https://github.com/eduardoagr/DuoNotes

MainPageViewModel

     public MainPageViewModel() {

            FireBaseNotebooks = new ObservableCollection<NotebookNote>();

            App.services = new FirebaseServices(FireBaseNotebooks);

            PageAppearCommand = new Command(AppearAction);

            LogoutCommand = new Command(LogOutAction);

            SeletedItemCommand = new Command(SeletedItemActionAsync);

            FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);

            ProfileCommnd = new Command(NavigateCommandAsync);

        }

        public virtual async void AppearAction() {
            App.services.ReadAsync(App.Notebooks);
            FireUser = await App.services.GetProfileInformationAndRefreshToken();


        }

        private async void NavigateCommandAsync() {
            await Application.Current.MainPage.Navigation.PushAsync(new ProfilePage());
        }

        private async void AnimateButtonCommand(Frame obj) {

            await obj.ScaleTo(0.8, 50, Easing.Linear);
            //Wait a moment
            await Task.Delay(100);
            //Scale to normal
            await obj.ScaleTo(1, 50, Easing.Linear);
            await PopupNavigation.Instance.PushAsync(new NotebookPopUp());

        }

        public async virtual void SeletedItemActionAsync() {

            if (SelectedNotebook == null) {
                return;
            }

            NotesPage notesPage = new NotesPage();
            MessagingCenter.Send(this, App.NotebookID, SelectedNotebook.Id);
            await Application.Current.MainPage.Navigation.PushAsync(notesPage);
            SelectedNotebook = null;


        }

NewNotebook PopUp

     public NewNotePopUpViewModel() {

            Services = App.services;

            Note = new Note {
                OnAnyPropertiesChanged = () => {

                    (NewNoteCommand as Command).ChangeCanExecute();
                }
            };

            NewNoteCommand = new Command(CreateNewNoteAsync, CanCreateNote);


            NotebookkIdAction = (id) => {

                NotebookId = id;
            };

        }

        private async void CreateNewNoteAsync() {
            if (Note == null) {
                return;
            }

            Note = new Note() {
                CreatedDate = DateTime.Now.ToString("D", new CultureInfo(App.languages)),
                NotebookId = NotebookId,
                Name = Note.Name,
                Id = Note.Id,
                FileLocation = string.Empty
            };

            await Services.InsertAsync(Note, App.Notes);
            await PopupNavigation.Instance.PopAsync();

            Services.ReadAsync(App.Notes, NotebookId);
        }


        private bool CanCreateNote() {

            return Note != null && !string.IsNullOrEmpty(Note.Name);
        }
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2022-02-10T09:29:06.227+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    When I run for the first time I can see all my notebooks, but if I select a note and come back to the MainPage and insert a new notebook, I cannot see it. It looks like it doesn't update the >observable

    This issue is related to observable do not update when you close the popup page. When popup page closed, your App.services.ReadAsync(App.Notebooks); need to be called.

    So I add a event in the NotebookPopUp background code. If the pop up page is closed, invoke this event in the MainPage's viewmodel. If you want to know more details about Raise event, please see : How to: Raise and Consume Events

       public partial class NotebookPopUp : PopupPage {  
               public NotebookPopUp() {  
                   InitializeComponent();  
               }  
                
         
               protected override bool OnBackButtonPressed() {  
                   return true;  
               }  
         
               //add this event  
               public event EventHandler PopPageClosed;  
         
               // If the pop up page is closed, invoke this event in the MainPage's viewmodel  
               protected override void OnDisappearing()  
               {  
                   PopPageClosed.Invoke(this, EventArgs.Empty);      
                   base.OnDisappearing();  
               }  
         
               protected override bool OnBackgroundClicked() {  
                   return false;  
               }  
           }  
    

    Then open your MainPage's viewmodel MainPageModel.cs. Find AnimateButtonCommand method. Register the event by notebookPopUp.PopPageClosed += NotebookPopUp_PopPageClosed; And add App.services.ReadAsync(App.Notebooks); code to NotebookPopUp_PopPageClosed method.

       private async void AnimateButtonCommand(Frame obj) {  
         
                   await obj.ScaleTo(0.8, 50, Easing.Linear);  
                    
                   NotebookPopUp notebookPopUp=  new NotebookPopUp();  
         
                   //register this event  
                   notebookPopUp.PopPageClosed += NotebookPopUp_PopPageClosed;      
         
         
                   await PopupNavigation.Instance.PushAsync(notebookPopUp);  
         
               }  
         
               //if the PopUp page closed, get the data again  
               private void NotebookPopUp_PopPageClosed(object sender, System.EventArgs e)  
               {  
                   App.services.ReadAsync(App.Notebooks);  
               }  
    

    Now, If you pop up page is closed. You can get the data again. Mainpage could run normally.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful