firebath数据库常见操作——增删改查使用总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了firebath数据库常见操作——增删改查使用总结相关的知识,希望对你有一定的参考价值。
firebath数据库常见操作——增删改查使用总结
问题背景
之前在看一个GitHub上的海外项目用到了Google的实时在线数据库firebath,查看官方文档提供的demo和api介绍感觉相对还是比较少的。现把之前用到的一些API进行简单总结,后续有需要会持续优化。
问题分析
项目对应module的gradle文件中添加响应依赖
// firebase platform dependencies
implementation platform(com.google.firebase:firebase-bom:30.4.1)
// authentication
implementation com.google.firebase:firebase-auth
//firestore
implementation com.google.firebase:firebase-firestore
//storage
implementation com.google.firebase:firebase-storage
implementation com.google.firebase:firebase-analytics
使用到的api梳理如下: 1、用户注册相关API使用
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class RegisterActivity extends AppCompatActivity
...
private FirebaseAuth mAuth;
private FirebaseFirestore db;
@Override
protected void onCreate(Bundle savedInstanceState)
...
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
...
/**
* todo: 注册
*/
protected void registerUser(String email,String password,String name)
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(task.isSuccessful())
FirebaseUser user = mAuth.getCurrentUser();
// set FirebaseUtil userId
FirebaseUtil.saveUserId(RegisterActivity.this, mAuth.getCurrentUser().getUid());
...
Map<String, Object> usermap = new HashMap<>();
usermap.put("displayName", name);
/**
* 注册成功初始化用户系信息到users表中
*/
db.collection("users")
.document(user.getUid())
.set(usermap)
.addOnSuccessListener(aVoid ->
)
.addOnFailureListener(e ->
);
...
else
...
);
...
2、查询firebath数据库相关API使用demo
public static void getEvent(String eventId, Handler handler)
Log.d(TAG, "getEvent begin");
FirebaseFirestore.getInstance().collection("events")
.document(eventId).get()
.addOnCompleteListener(task ->
if (task.isSuccessful())
Log.d(TAG, "getEvent isSuccessful" + task.getResult().getData().toString());
Event event1 = null;
try
event1 = (Event) JsonUtil.mapToObject(task.getResult().getData(), Event.class);
catch (Exception e)
e.printStackTrace();
Message message = Message.obtain();
message.obj = event1;
message.what = 1;
handler.sendMessage(message);
else
Log.e(TAG, "getEvent complete failed");
).addOnFailureListener(e ->
Log.d(TAG, "getEvent Failure");
);
// return event.get();
3、修改firebath数据库某数据字段API使用demo 先查询到数据,然后重新赋值。
public static void cancelEvent(String eventId)
Log.d(TAG, "getEvent begin");
FirebaseFirestore.getInstance().collection("events")
.document(eventId).get()
.addOnCompleteListener(task ->
if (task.isSuccessful())
Log.d(TAG, "getEvent isSuccessful" + task.getResult().getData().toString());
Event event1 = null;
try
event1 = (Event) JsonUtil.mapToObject(task.getResult().getData(), Event.class);
catch (Exception e)
e.printStackTrace();
event1.setStatus("canceled");
FirebaseFirestore.getInstance().collection("events")
.document(eventId).set(event1);
else
Log.e(TAG, "getEvent complete failed");
).addOnFailureListener(e ->
Log.d(TAG, "getEvent Failure");
);
// return event.get();
4、插入数据到firebath数据库API使用demo
public static void insertNotificationMap(Map<String, Object> notificationMap)
Log.d(TAG, "insertNotificationMap begin");
FirebaseFirestore.getInstance().collection("notifications")
.document()
.set(notificationMap)
.addOnCompleteListener(task ->
...
)
.addOnSuccessListener(aVoid ->
...
).addOnFailureListener(e ->
...
);
5、删除firebath数据库某条数据API使用demo
public static void deleteNotificationMap(String documentId)
Log.d(TAG, "deleteNotificationMap begin");
FirebaseFirestore.getInstance().collection("notifications")
.document(documentId).delete()
.addOnCompleteListener(task ->
Log.d(TAG, "deleteNotificationMap Complete");
if (task.isSuccessful())
...
else
...
)
.addOnSuccessListener(aVoid ->
...
).addOnFailureListener(e ->
...
);
问题总结 本文梳理了部分之前使用到的firebath相关的增删改查相关API操作,后面有需要会持续优化。
以上是关于firebath数据库常见操作——增删改查使用总结的主要内容,如果未能解决你的问题,请参考以下文章