Firebase reads the wrong data

Eduardo Gomez 3,651 Reputation points
2022-02-26T11:19:15.2+00:00

Hello

I have my notebooksPage, witch read all my notebooks, but when I navigate to my Notes page, for some reason it read the notebooks again, this is becouse on inheritance

NotebookPageModel

  public async virtual void SeletedItemActionAsync() {  
  
            if (SelectedNotebook != null) {  
  
                NotesPage notesPage = new NotesPage();  
                Application.Current.Properties["id"] = SelectedNotebook.Id;  
                await Application.Current.MainPage.Navigation.PushAsync(notesPage, true);      
                SelectedNotebook = null;  
            }  
        }  

Here I m passing the selected Notobook selected id to the NotesPage model

NotesPageModel

 public class NotesPageModel : NotebooksPageModel {  
  
        public string NotebookId { get; set; }  
  
        public Note SeletedNote { get; set; }  
  
        public Command<Note> DeleteNoteCommand { get; set; }  
  
        public Command PageDisappearCommand { get; set; }  
  
        public NotesPageModel() {  
  
            DeleteNoteCommand = new Command<Note>(DeleteNoteAction);  
  
            FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);  
  
           // PageDisappearCommand = new Command(PageDisappearAction);  
        }  
  
  
  
        public override async void AppearAction() {  
            base.AppearAction();  
  
            NotebookId = Application.Current.Properties["id"] as string;  
  
            Console.WriteLine($"NotebookID {NotebookId}");  
  
            FireBaseNotebookNotes = await App.FirebaseServices.ReadAsync(AppConstant.Notes, NotebookId);  
  
        }  

          

This method fires every time the page appears, and I can verify I have the ID

177910-image.png

ReadAsync

      public async Task<ObservableCollection<NotebookNote>> ReadAsync(string ChildName, string NotebookId = "") {  
  
            var list = await firebaseClient.Child(ChildName)  
                 .OnceAsync<NotebookNote>();  
  
            var collection = new List<NotebookNote>();  
  
            foreach (var item in list) {  
                NotebookNote notebookNote = null;  
                notebookNote = Convert(ChildName, item);  
                collection.Add(notebookNote);  
            }  
  
            if (ChildName.Equals(AppConstant.Notes)) {  
                collection = collection.Where(n => ((Note)n).NotebookId == NotebookId).ToList();  
            } else {  
                collection = collection.Where(n => n.UserID == Preferences.Get(AppConstant.UserID, string.Empty)).ToList();  
            }  
            FireBaseNotebooks.Clear();  
            foreach (var element in collection) {  
                FireBaseNotebooks.Add(element);  
            }  
  
            return FireBaseNotebooks;  
        }  

https://github.com/eduardoagr/DuoNotes

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-02-28T07:01:03.003+00:00

    Hello,​

    I debug your application, When I navigate the NotesPage, await App.FirebaseServices.ReadAsync(AppConstant.Notes, NotebookId); method will be executed twice.

    I can get the correct result at the first time, but I get the wrong result at the second time.

    So, I remove the base.AppearAction(); in the AppearAction method of NotesPageModel.cs, the result the correct. Because your NotesPageModel extend NotebooksPageModel, and you will execute the NotebooksPageModel's AppearAction again.

    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 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.