firestore vs firebase database

Choose a Database: Cloud Firestore or Realtime Database

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing:

  • Realtime Database is Firebase's original database. It's an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.
  • Cloud Firestore is Firebase's new flagship database for mobile app development. It improves on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales better than the Realtime Database.

Which database is right for your project?

If you're comfortable with a product in beta, use Cloud Firestore for your new projects. Cloud Firestore offers additional functionality, performance, and scalability on an infrastructure designed to support more powerful features in future releases. Expect to see new query types, more robust security rules, and improvements to performance among the advanced features planned for Cloud Firestore.

As you're choosing between database solutions, consider the following differences between Cloud Firestore and the Realtime Database.

Data model

Both Realtime Database and Cloud Firestore are NoSQL Databases.

Realtime DatabaseCloud Firestore
Stores data as one large JSON tree.
  • Simple data is very easy to store.
  • Complex, hierarchical data is harder to organize at scale.

Learn more about the Realtime Database data model.

Stores data in documents organized in collections.
  • Simple data is easy to store in documents, which are very similar to JSON.
  • Complex, hierarchical data is easier to organize at scale, using subcollections within documents.
  • Requires less denormalization and data flattening.

Learn more about the Cloud Firestore data model.

Realtime and offline support

Both have mobile-first, realtime SDKs and both support local data storage for offline-ready apps.

Realtime DatabaseCloud Firestore
Offline support for mobile clients on iOS and Android only.Offline support for iOS, Android, and web clients.

Querying

Retrieve, sort, and filter data from either database through queries.

Realtime DatabaseCloud Firestore
Deep queries with limited sorting and filtering functionality.
  • You can only sort or filter on a property, not sort andfilter on a property, in a single query.
  • Queries are deep by default: They always return the entire subtree.
Indexed queries with compound sorting and filtering.
  • You can chain filters and combine filtering and sorting on a property in a single query.
  • Write shallow queries for subcollections: You can query subcollections within a document instead of an entire collection, or even an entire document.
  • Queries are indexed by default: Query performance is proportional to the size of your result set, not your data set.

Writes and transactions

Realtime DatabaseCloud Firestore
Basic write and transaction operations. Atomic write and transaction operations.

Reliability and performance

Realtime DatabaseCloud Firestore
Realtime Database is a mature product.
  • Stability you'd expect from a battle-tested, tried-and-true product.
  • Very low latency, so it's a great option for frequent state-syncing.
  • Databases are limited to zonal availability in a single region.
Read more about Realtime Database performance and reliability characteristics in the Service Level Agreement.
Cloud Firestore is currently in beta.
  • Stability in a beta product is not always the same as that of a fully launched product.
  • Houses your data across multiple data centers in distinct regions, ensuring global scalability and strong reliability.
  • When Cloud Firestore graduates from beta, it will have stronger reliability than Realtime Database.

Scalability

Realtime DatabaseCloud Firestore
Scaling requires sharding.

Scale to around 100,000 concurrent connections and 1,000 writes/second in a single database. Scaling beyond that requires sharding your data across multiple databases.

Scaling will be automatic.

Scales completely automatically (after beta), meaning you don't need to shard your data across multiple instances.

Security

Realtime DatabaseCloud Firestore
Cascading rules that require separate validation. Simpler, more powerful security for mobile, web, and server SDKs.

Pricing

Both solutions are available on the Spark, Flame, and Blaze pricing plans.

Realtime DatabaseCloud Firestore
Charges only for bandwidth and storage, but at a higher rate.

Read more about Realtime Database pricing plans.

Charges primarily on operations performed in your database (read, write, delete) and, at a lower rate, bandwidth and storage.

Cloud Firestore supports daily spending limits for Google App Engine projects, to make sure you don't go over the costs you're comfortable with.

Read more about Cloud Firestore pricing plans.

Using Cloud Firestore and Realtime Database

You can use both databases within the same Firebase app or project. Both NoSQL databases can store the same types of data and the client libraries work in a similar manner. Keep in mind the differences outlined above if you decide to use both databases in your app.

If you want to try out Cloud Firestore while it's in beta, use our getting started guide.

To learn more about the features available on the Realtime Database, take a look at the Realtime Database documentation.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现失物招领的 Android 应用可以分为以下几步: 1. 设计 UI 界面,包括主界面、发布信息界面、查看信息界面等。 2. 实现用户注册、登录功能。 3. 实现信息发布和查询功能。 4. 使用数据库存储发布的信息,包括失物招领信息和拾物信息。 5. 添加图片上传功能,让用户能够上传图片以更好地描述失物或拾物。 6. 实现信息推送功能,让用户能够收到有关他们发布的信息的通知。 以下是一个简单的实现思路和代码示例: 1. 设计 UI 界面:可以使用 Android Studio 提供的布局编辑器设计应用的 UI 界面,包括主界面、发布信息界面、查看信息界面等。 2. 实现用户注册、登录功能:可以使用 Firebase Authentication 来实现用户的注册和登录功能。具体的实现步骤可以参考 Firebase Authentication 官方文档。 3. 实现信息发布和查询功能:可以使用 Firebase Realtime DatabaseFirebase Cloud Firestore 来实现信息的发布和查询功能。具体的实现步骤可以参考 Firebase Realtime DatabaseFirebase Cloud Firestore 官方文档。 下面是一个使用 Firebase Realtime Database 来实现信息发布和查询功能的示例代码: ```java // 获取 Firebase 数据库实例 FirebaseDatabase database = FirebaseDatabase.getInstance(); // 获取失物招领信息存储引用 DatabaseReference lostRef = database.getReference("lost"); // 获取拾物信息存储引用 DatabaseReference foundRef = database.getReference("found"); // 发布失物招领信息 public void publishLost(String title, String description, String imageUrl) { // 创建一个新的失物招领信息 LostItem item = new LostItem(title, description, imageUrl); // 将新的失物招领信息存储到 Firebase 数据库中 lostRef.push().setValue(item); } // 发布拾物信息 public void publishFound(String title, String description, String imageUrl) { // 创建一个新的拾物信息 FoundItem item = new FoundItem(title, description, imageUrl); // 将新的拾物信息存储到 Firebase 数据库中 foundRef.push().setValue(item); } // 查询失物招领信息 public void queryLost(final DataListener<List<LostItem>> listener) { // 从 Firebase 数据库中获取失物招领信息 lostRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // 将获取到的失物招领信息转换为 LostItem 列表 List<LostItem> items = new ArrayList<>(); for (DataSnapshot snapshot : dataSnapshot.getChildren()) { LostItem item = snapshot.getValue(LostItem.class); items.add(item); } // 通知监听器获取到了失物招领信息 listener.onDataLoaded(items); } @Override public void onCancelled(DatabaseError databaseError) { // 处理查询失物招领信息时出现的错误 listener.onError(databaseError.getMessage()); } }); } // 查询拾物信息 public void queryFound(final DataListener<List<FoundItem>> listener) { // 从 Firebase 数据库中获取拾物信息 foundRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // 将获取到的拾物信息转换为 FoundItem 列表 List<FoundItem> items = new ArrayList<>(); for (DataSnapshot snapshot : dataSnapshot.getChildren()) { FoundItem item = snapshot.getValue(FoundItem.class); items.add(item); } // 通知监听器获取到了拾物信息 listener.onDataLoaded(items); } @Override public void onCancelled(DatabaseError databaseError) { // 处理查询拾物信息时出现的错误 listener.onError(databaseError.getMessage()); } }); } // 失物招领信息类 public class LostItem { public String title; public String description; public String imageUrl; public LostItem() { // 默认构造函数必须存在,否则在从 Firebase 数据库中读取数据时会报错 } public LostItem(String title, String description, String imageUrl) { this.title = title; this.description = description; this.imageUrl = imageUrl; } } // 拾物信息类 public class FoundItem { public String title; public String description; public String imageUrl; public FoundItem() { // 默认构造函数必须存在,否则在从 Firebase 数据库中读取数据时会报错 } public FoundItem(String title, String description, String imageUrl) { this.title = title; this.description = description; this.imageUrl = imageUrl; } } // 数据监听器接口 public interface DataListener<T> { void onDataLoaded(T data); void onError(String message); } ``` 4. 使用数据库存储发布的信息:在上面的示例代码中,我们使用了 Firebase Realtime Database 来存储发布的失物招领信息和拾物信息。具体的实现步骤可以参考 Firebase Realtime Database 官方文档。 5. 添加图片上传功能:可以使用 Firebase Storage 来实现图片上传功能。具体的实现步骤可以参考 Firebase Storage 官方文档。 下面是一个使用 Firebase Storage 来实现图片上传功能的示例代码: ```java // 获取 Firebase 存储实例 FirebaseStorage storage = FirebaseStorage.getInstance(); // 获取图片存储引用 StorageReference imagesRef = storage.getReference().child("images"); // 上传图片 public void uploadImage(Uri imageUri, final DataListener<String> listener) { // 获取图片的文件名 String filename = UUID.randomUUID().toString(); // 创建一个新的图片存储引用 StorageReference imageRef = imagesRef.child(filename); // 上传图片到 Firebase 存储 imageRef.putFile(imageUri) .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { // 获取上传后的图片地址 imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // 通知监听器上传成功,并返回图片地址 listener.onDataLoaded(uri.toString()); } }); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // 处理上传图片失败的情况 listener.onError(e.getMessage()); } }); } ``` 6. 实现信息推送功能:可以使用 Firebase Cloud Messaging 来实现信息推送功能。具体的实现步骤可以参考 Firebase Cloud Messaging 官方文档。 以上是一个简单的失物招领 Android 应用的实现思路和代码示例。当然,具体的实现方式还需要根据应用的需求做出相应的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值