I am new to Firebase and I am planning to switch from SQLite to Firebase.
I have 2 tables in SQLite.
First one is NAME OF REPOSITORIES
. This will will be general for all users which is accessible to all the users of the application.
Second one is BOOKMARKS
. This will be specific to a particular user . I know that there is no table structure in Firebase .
Question is, how can I structure this type of data in Firebase? I have tried understanding tree structure of Firebase, but didn't get help for this type of scenario.
To create two tables you have to create object of DatabaseReference
for second table. Also you have to create second node.
public class MainActivity extends AppCompatActivity {
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
//Create an object of DatabaseReference to create second table
private DatabaseReference mFirebaseDatabase1;
private String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseInstance = FirebaseDatabase.getInstance();
// get reference to 'RepositoryName' node
mFirebaseDatabase = mFirebaseInstance.getReference("RepositoryName");
// get reference to 'Bookmarks' node
mFirebaseDatabase1 = mFirebaseInstance.getReference("Bookmarks");
// Save / update the user
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = inputName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
String confirmPassword = inputConfirmPassword.getText().toString();
// Check for already existed userId
if (TextUtils.isEmpty(userId)) {
createUser(name, email, password, confirmPassword);
}
}
});
}
/**
* Creating new user node under 'users'
*/
private void createUser(String name, String email, String password, String confirmPassword) {
// TODO
// In real apps this userId should be fetched
// by implementing firebase auth
if (TextUtils.isEmpty(userId)) {
/*userId store the unique key like KYNGnlMMIf3w11VukqD
in this key store usename and email as JSON format in firebase cloud database*/
// "mFirebaseDatabase" is for table1(i.e RepositoryName)
//userId = mFirebaseDatabase.push().getKey();
// "mFirebaseDatabase1" is for table2(i.e Bookmarks)
userId = mFirebaseDatabase1.push().getKey();
}
User user = new User(name, email, password, confirmPassword);
//insert data in firebase database RepositoryName
mFirebaseDatabase.child(userId).setValue(user);
//insert data in firebase database Bookmarks
mFirebaseDatabase1.child(userId).setValue(user);
}
}
Hope this helps you :)
Hello at the first you should try with understanding the basic concept of denormalizing data. You can check one of the blog post.
Again, for more information of stucturing data in detail is here.
You can start structuring data using these links again. You have to have denormalised structure to developer best experience in firebase.
Coming to you point, Two tables.
You need to create 2 nodes in firebase. First one is with NAME OF REPOSITORIES
and BOOKMARKS
. If you are having any relations for both the table create a new node with that relation lets say USERBOOKMARK
and try adding reference to the node.