locked
Firebase Database xamarin implementation RRS feed

  • Question

  • User16698 posted

    Hey guys, is there a way to write data into firebase database without using wrapper packages?

    I keep getting error no properties to serialize found with this code:

    DatabaseReference mDatabase = FirebaseDatabase.Instance.Reference;
    mDatabase.Child("accounts").Child(currentKey).SetValue(newAccount);
    
    Friday, December 16, 2016 5:05 PM

All replies

  • User59234 posted

    I've got the same issue, @MarioFumagalli did you manage to solve it?

    Thursday, December 22, 2016 2:25 AM
  • User16698 posted

    @KenNicholl not yet.

    Thursday, December 29, 2016 9:27 AM
  • User59234 posted

    Hi @MarioFumagalli

    Not sure if you're still trying to get this to work, but I have managed to get it working by doing my own mapping.

    So instead of trying to get the Firebase library to serialize/deserialize my POCO classes, I instead created a map of each POCO to map to and from a Java.Util.HashMap.

    So to save data, in this case a new contact...

    var contactsRef = App.Current.FirebaseDatabase.GetReference(Settings.GetContactRef(App.Current.CurrentUserId));
    
    //save new contact
    _contact = new ContactModel
     {
        Uid = Guid.NewGuid().ToString(),  //temp key
        FirstName = _txtFirstName.Text.Trim(),
        LastName = _txtLastName.Text.Trim(),
        PhoneNumber = _txtPhone.Text.Trim(),
        DefaultContact = _swPrimary.Checked
    };
    contactsRef.Push().SetValue(_contact.ContactModelToMap()).AddOnCompleteListener(this);
    

    Then to map data received from Firebase...

    ...
    var contactsRef = App.Current.FirebaseDatabase.GetReference(Settings.GetContactRef(App.Current.CurrentUserId));
                contactsRef.AddValueEventListener(this);
    ...
    
    public void OnDataChange(DataSnapshot snapshot)
    {
        _contacts = new List<ContactModel>();
        var obj = snapshot.Children;
        foreach (DataSnapshot s in obj.ToEnumerable())
        {
            var model = s.MapToContactModel();
    
            if (model != null)
                _contacts.Add(model);
        }
    }
    

    Note that you need to implement IValueEventListener on your Activity/Fragment

    And here's my code for mapping a POCO to a HashMap, and a DataSnapShot to a POCO.

    public static HashMap ContactModelToMap(this ContactModel model)
            {
                HashMap map = new HashMap();
                map.Put("Uid", model.uId);
                map.Put("FirstName", model.FirstName);
                map.Put("LastName", model.LastName);
                map.Put("PhoneNumber", model.PhoneNumber);
                map.Put("DefaultContact", model.DefaultContact);
    
                return map;
            }
    
            public static ContactModel MapToContactModel(this DataSnapshot snapShot)
            {
                ContactModel model = new ContactModel();
    
                if (snapShot.GetValue(true) == null) return null; // key, but no value, recently deleted. Return null.
    
                model.uId = snapShot.Key;
                model.FirstName = snapShot.Child("FirstName")?.GetValue(true)?.ToString();
                model.LastName = snapShot.Child("LastName")?.GetValue(true)?.ToString();
                model.PhoneNumber = snapShot.Child("PhoneNumber")?.GetValue(true)?.ToString();
    
                bool defaultContact;
                bool.TryParse(snapShot.Child("DefaultContact")?.GetValue(true).ToString(), out defaultContact);
                model.DefaultContact = defaultContact;
    
                return model;
            }
    

    This is obviously not the most efficient implementation, but it seems to be adequate for my app. Also supports offline use out-of-the-box which is cool.

    Now to try with iOS....

    Thursday, January 5, 2017 9:07 AM
  • User108245 posted

    @KenNicholl you saved me. thank you for your solution

    Sunday, October 21, 2018 7:44 PM