Reputation: 63
Ok, so I am building xamarin.android applicationon in Visual Studio and there is quite a lot of questions of this type, but I really didn't find anything regarding xamarin.android, everything is Java based. I tried following those tutorials and answers but there was always something missing or not working on Xamarin android.
I have made authentication and it works great, no problems there. And i tried storing user information (info that user types in while registering) into database. It kind of worked, but with one problem. Here is code:
public void OnComplete(Task task)
{
if (task.IsSuccessful)
{
Toast.MakeText(this, "Successful", ToastLength.Long).Show();
FirebaseUser user = FirebaseAuth.GetInstance(app).CurrentUser;
id = user.Uid;
CreateUser();
buttonSignIn.PerformClick();
progressBar.Visibility = ViewStates.Invisible;
}
else
{
//something
}
}
And here is CreateUser()
method:
private void CreateUser ()
{
Account user = new Account();
user.uid = id;
user.name = signup_inputName;
user.lastName = signup_inputLastName;
user.email = signup_inputEmail;
user.phone = signup_inputPhoneNumber;
var firebase = new FirebaseClient(FirebaseURL);
var item = firebase.Child("users").PostAsync<Account>(user);
}
Here is Account class code:
public class Account
{
public string uid { get; set; }
public string name { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
This code stores user information under "users"
node in database. But there is one more node under "user"
with some random value and below are information about user (including uid
). Here is output:
- Users
-LBGFtYFTfD3l1hmwHVn
email: "[email protected]"
lastName:"peric"
name: "pero"
phone: "12321"
uid: "18puc5CzSZfzbdflzekzNCHGHR62"
So, my question is, shouldn't this random value below "users"
node be uid
? If yes, how to set it that way?
I tried with this:
Account user = new Account();
user.uid = id;
user.name = signup_inputName;
user.lastName = signup_inputLastName;
user.email = signup_inputEmail;
user.phone = signup_inputPhoneNumber;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users").Child(uid).SetValue(user)
But that didn't work, I was getting this error:
Firebase No properties to serialize found on class
Even though I have set public getters and setters in users class.
I need to get that user info and show it on some places in my app, but I just can't get it to work. This is my first time using Firebase and I am used to SQL database so this is really confusing to me. Also, there is very little information online about xamarin.android and firebase, plus I am actually new to programming in general. Any help would be appreciate. This is my last try before switching to SQL online database.
Upvotes: 0
Views: 962
Reputation: 599766
When you call PostAsync
the client creates a new child node under the location that you call it on. This is similar to the HTTP POST
verb, and the push()
method in most other Firebase SDKs.
To write data to a location that you exactly specify, use PutAsync
:
var item = firebase.Child("users").Child(uid).PutAsync<Account>(user);
See the example from the Readme of Firebase.Xamarin.
Upvotes: 1