我是Firebase的新手,我打算从SQLite切换到Firebase。
我在SQLite中有两个表。
第一个是NAME OF REPOSITORIES
。对于应用程序的所有用户都可以访问的所有用户,这将是通用的。
第二个是BOOKMARKS
。这将特定于特定用户。我知道Firebase中没有表结构。
问题是,如何在Firebase中构建此类数据?我已经尝试了解Firebase的树结构,但没有得到这种情况的帮助。
答案 0 :(得分:3)
要创建两个表,您必须为第二个表创建DatabaseReference
的对象。您还必须创建第二个节点。
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);
}
}
希望这可以帮助你:)
答案 1 :(得分:1)