rafaelqfigueiredo
rafaelqfigueiredo

Reputation: 87

Xamarin.Forms: DependencyService to get Firebase Android data

I'm using Xamarin.Forms to implement the same UI to iOS and Android but I have to implement functions that read and write data to Firebase separately (they are different).

I want to put all the data from Firebase to an ObservableCollection, like this:

ObservableCollection<Post> posts;
posts = DependencyService.Get<IFeed>().getPosts();

And the Android code that is called is:

    ObservableCollection<Post> posts = new ObservableCollection<Post>();

    public FeedAndroid() 
    { 
        database = FirebaseDatabase.GetInstance(MainActivity.app);
        dataRef = database.Reference;
        postsRef = dataRef.Child("posts");

        posts.Clear();

        postsRef.AddChildEventListener(this);
    }

    public void OnChildAdded(DataSnapshot snapshot, string previousChildName)
    {
        Post newPost = new Post { Title = snapshot.Child("title")?.GetValue(true)?.ToString(),
        Desc = snapshot.Child("desc")?.GetValue(true)?.ToString(),
        Img = snapshot.Child("image")?.GetValue(true)?.ToString()};

        posts.Add(newPost);
    }

    public ObservableCollection<Post> getPosts()
    {
        return posts;
    }

But this doesn't work. Any idea of what to do?

Upvotes: 1

Views: 588

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

I guess that your FeedAndroid() is the constructor of your class which implement your IFeed interface. Your FirebaseDatabase instance is created in this constructor, which may be the problem here. I would suggest to implement it in MainActivity. For example:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IChildEventListener
{
    public DatabaseReference postsRef;
    public static ObservableCollection<Post> collection = new ObservableCollection<Post>();

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }

    protected override void OnResume()
    {
        base.OnResume();

        FirebaseApp.InitializeApp(this);

        FirebaseAuth mAuth = FirebaseAuth.Instance;
        FirebaseUser user = mAuth.CurrentUser;
        if (user == null)
        {
            var result = mAuth.SignInAnonymously();
        }

        postsRef = FirebaseDatabase.Instance.Reference.Child("posts");
        postsRef.AddChildEventListener(this);
    }

    public void OnCancelled(DatabaseError error)
    {
        //TODO:
    }

    public void OnChildAdded(DataSnapshot snapshot, string previousChildName)
    {
        collection.Add(new Post() {//Your Data here});
    }

    public void OnChildChanged(DataSnapshot snapshot, string previousChildName)
    {
        //TODO:
    }

    public void OnChildMoved(DataSnapshot snapshot, string previousChildName)
    {
        //TODO:
    }

    public void OnChildRemoved(DataSnapshot snapshot)
    {
        //TODO:
    }
}

And in your FeedAndroid class simply return this collection:

public ObservableCollection<Post> getPosts()
{
    return MainActivity.collection;
}

Upvotes: 1

Related Questions