firebase创建数据库_如何使用Firebase Firestore设置数据库以与android体系结构组件一起使用...

本文介绍了如何将Firebase Firestore集成到Android应用中,作为架构组件的一部分,详细阐述了创建和使用Firebase数据库的步骤。
摘要由CSDN通过智能技术生成

firebase创建数据库

In a previous article, we discussed how to create an Android app with two of the most important building blocks of Android Architecture Components: LiveData and ViewModel. This is a follow-up to that tutorial.

上一篇文章中 ,我们讨论了如何使用Android体系结构组件的两个最重要的构建块来创建Android应用程序:LiveData和ViewModel。 这是该教程的后续内容。

Once we have an app with clean architecture, we will be configuring Firebase Firestore for Android Architecture Components to update data in real-time and make the most out of LiveData and ViewModel in Android.

有了拥有干净架构的应用程序后,我们将为Android体系结构组件配置Firebase Firestore,以实时更新数据并充分利用Android中的LiveData和ViewModel。

启动 (Starting Up)

We will start this tutorial with the app we built for the previous tutorial, My Shopping List. Download the starter project from my website.

我们将从为上一教程构建的应用程序“我的购物清单”开始本教程。 从我的网站下载入门项目。

Open the app in Android Studio and let the Gradle sync finish. Run the app and you should see an app with a shopping list:

在Android Studio中打开该应用,然后让Gradle同步完成。 运行该应用程序,您应该会看到一个带有购物清单的应用程序:

Image for post
Image 1: My Shopping List app.
图片1:“我的购物清单”应用。

在Firebase控制台中创建项目 (Creating a Project in the Firebase Console)

The first step is to create a Firebase Console Project. Go to the Firebase Console main page. On this screen, you will get one of two layouts:

第一步是创建Firebase控制台项目。 转到Firebase控制台主页 。 在此屏幕上,您将获得两种布局之一:

  1. If you have never used the Firebase Console before, you will be prompted with a welcome message and an invitation to create a project.

    如果您以前从未使用过Firebase控制台,则系统将提示您一条欢迎消息和创建项目的邀请。
Image for post
Image 2: Firebase Console welcome screen with no projects.
图2:没有项目的Firebase控制台欢迎屏幕。

2. If you have other projects, you will see a list of them and a white card to add a project.

2.如果您还有其他项目,则将看到其中的列表和一张添加项目的白卡。

Image for post
Image 3: Firebase Console welcome screen with other projects.
图3:Firebase Console欢迎屏幕以及其他项目。

Click the button to create a project depending on the option you have and you will start the flow for creating a project. The first screen will prompt you for the name of the project. In this case, you will use the same name of your Android app, which is MyShoppingList.

单击按钮创建一个项目,具体取决于您拥有的选项,然后将开始创建项目的流程。 第一个屏幕将提示您输入项目名称。 在这种情况下,您将使用与Android应用程序相同的名称,即MyShoppingList。

Image for post
Image 4: Naming the Firebase project.
图4:命名Firebase项目。

Click Continue and the next step will ask you whether you want to enable Google Analytics. This is not necessary for Firestore, but you may want to enable it for other reasons:

点击继续,下一步将询问您是否要启用Google Analytics(分析)。 对于Firestore,这不是必需的,但是您可能由于其他原因而希望启用它:

Image for post
Image 5: Enabling Google Analytics in the Firebase project.
图片5:在Firebase项目中启用Google Analytics(分析)。

Click Create project and you will get a loading screen (Image 6). It will take a small amount of time, so don’t worry.

单击创建项目,您将获得一个加载屏幕(图6)。 这将花费少量时间,所以请不要担心。

Image for post
Image 6: Creating Firebase project loader.
图6:创建Firebase项目加载器。

Once the process is finalized, the loader will turn into three orange dots with a confirmation text and a Continue button will appear.

完成该过程后,加载程序将变为带有确认文本的三个橙色点,并且将显示继续按钮。

Image for post
Image 7: Firebase Project created correctly.
图片7:Firebase项目已正确创建。

Click on the Continue button and you will be taken to the main screen of the MyShoppingList project.

单击继续按钮,您将转到MyShoppingList项目的主屏幕。

Image for post
Image 8: Main screen of MyShoppingList project.
图像8:MyShoppingList项目的主屏幕。

It is important to understand that this project will work for many platforms — not only Android. If you were going to integrate Firestore to an iOS app or a web project, you would use the same project and this would also enable you to share the database across platforms.

重要的是要了解该项目将适用于许多平台-不仅适用于Android。 如果要将Firestore集成到iOS应用程序或Web项目,则将使用同一项目,这也使您能够跨平台共享数据库。

将Firebase添加到Android应用 (Adding Firebase to the Android App)

From this part on, the configuration is specific to Android. On the home screen of your project, click on the little white Android icon to start the integration flow.

从这一部分开始,该配置特定于Android。 在项目的主屏幕上,单击白色的Android小图标以启动集成流程。

Image for post
Image 9: Starting integration flow.
图9:开始集成流程。

As a first step, Firebase will request your app’s data. Add the following:

第一步,Firebase将请求您的应用程序数据。 添加以下内容:

  1. Package name: com.evanamargain.android.myshoppinglist. This has to be the same package name that you have in your app. If you are not sure and you are not using the demo project, go to app > manifests > AndroidManifest.xml. The package will be in the first few lines of the file.

    套件名称: com.evanamargain.android.myshoppinglist 。 该名称必须与您的应用中的软件包名称相同。 如果不确定并且不使用演示项目,请转到app> manifests> AndroidManifest.xml。 该软件包将在文件的前几行中。

  2. Nickname: My Shopping List. This can be whatever nickname that will help you remember the app name. Maybe you will want to add the suffix “Android” if you want to distinguish it from another app.

    昵称:我的购物清单。 可以使用任何昵称来帮助您记住应用程序名称。 如果您想将后缀“ Android”与其他应用程序区分开,则可能需要添加后缀“ Android”。
  3. SHA-1 certificate: This can be empty, as you won’t be using it for this tutorial.

    SHA-1证书:此证书可以为空,因为本教程不会使用它。
Image for post
Image 10. The first step with the needed data added.
图10.第一步,其中添加了所需的数据。

After inputting the data, you will get a JSON file named google-services.json. The screen has instructions to integrate it into the app, but I will explain below in case it is not clear.

输入数据后,您将获得一个名为google-services.json的JSON文件。 屏幕上有将其集成到应用程序中的说明,但是如果不清楚,我将在下面进行说明。

Image for post
Image 11: JSON config file for Firebase in Android.
图片11:Android中Firebase的JSON配置文件。

Go to Android Studio, where you have the project open, and switch the view in the left panel. Your project will likely be in “Android” mode and you will get a dropdown with the view options like in Image 12 below:

转到打开了项目的Android Studio,然后在左侧面板中切换视图。 您的项目可能处于“ Android”模式,并且您会看到一个下拉菜单,如下图12所示:

Image for post
Image 12: Android Project view in Android Studio.
图片12:Android Studio中的Android Project视图。

Once you are in the Project view, you will see the file tree displayed like in the image below. Download the file from the Firebase Console to any location of your computer and then drag and drop to MyShoppingList > app.

进入“项目”视图后,您将看到显示的文件树,如下图所示。 从Firebase控制台将文件下载到计算机的任何位置,然后拖放到MyShoppingList> app。

Image for post
Image 13: Android project structure.
图片13:Android项目结构。

Then switch back to the Android view in the left panel. Most of the time, you will be using Android projects in this view, as it is the easiest to navigate while coding.

然后,切换回左侧面板中的Android视图。 在大多数情况下,您将在此视图中使用Android项目,因为它是编码时最容易浏览的项目。

Image for post
Image 14: Android view in Android Studio.
图片14:Android Studio中的Android视图。

Once you get to the Android view, open Gradle Scripts > build.gradle (Project: MyShoppingList). Add this code:

进入Android视图后,打开Gradle脚本> build.gradle (Project: MyShoppingList) 。 添加此代码:

classpath 'com.google.gms:google-services:4.3.3'

In the same folder, open Build.gradle (Module: app) and add the following code:

在同一文件夹中, open Build.gradle (Module: app)并添加以下代码:

apply plugin: 'com.google.gms.google-services' 
... dependencies {
... // In case you activated Google Analytics // Add the Firebase SDK for Google Analytics implementation 'com.google.firebase:firebase-analytics:17.2.2' // Add Firestore dependency implementation 'com.google.firebase:firebase-firestore:21.}
Image for post
Image 15: Android Studio requesting a Gradle sync.
图片15:Android Studio请求Gradle同步。

If you have any doubts about whether the sync was successful, take a look at the bottom panel. It should look like Image 16 below:

如果您对同步是否成功有任何疑问,请查看底部面板。 它看起来应该像下面的图像16:

Image for post
Image 16: Gradle build successful in Android Studio.
图16:在Android Studio中Gradle构建成功。

完成Firebase控制台配置 (Finish Firebase Console Configuration)

Then go back to the Firebase Console and click Next to confirm you have added the JSON file and dependencies to the project. There will be a note at the bottom of the screen indicating that Firebase is verifying the connectivity between your app and the console. Build and run the app. It should run and look the same as it did at the beginning of the tutorial. After doing this, Firebase should confirm connectivity and give you a Continue to console button.

然后返回Firebase控制台并单击“下一步”以确认您已将JSON文件和依赖项添加到项目中。 屏幕底部将显示一条注释,指示Firebase正在验证您的应用程序与控制台之间的连接。 生成并运行该应用程序。 它应该运行并与本教程开始时的外观相同。 完成此操作后,Firebase应确认连接并为您提供继续控制台按钮。

Image for post
Image 17: Connectivity between the app and Firebase Console was confirmed.
图片17:应用程序和Firebase控制台之间的连接已确认。

If the step above fails, go back to the instructions and ensure that you did everything correctly. Otherwise, on the main screen of the project, you will see a purple icon with the Android logo in purple, which means your project is connected to the app.

如果以上步骤失败,请返回说明并确保您正确执行了所有操作。 否则,在项目的主屏幕上,您将看到一个紫色图标,其Android徽标为紫色,这意味着您的项目已连接到应用程序。

Image for post
Image 18: Android app added to Firebase Console project.
图片18:Android应用程序已添加到Firebase Console项目。

在Firebase控制台中配置Firestore (Configuring Firestore in Firebase Console)

Once you have a project in Firebase Console and have connected your app to the project, it’s time to configure your Firestore database in the console. Locate a big orange tile below the main project screen that looks like Image 19 below:

在Firebase控制台中拥有一个项目并将您的应用程序连接到该项目之后,就该在控制台中配置Firestore数据库了。 在主项目屏幕下方找到一个大的橙色瓷砖,如下图19所示:

Image for post
Image 19: Cloud Firestore access tile.
图片19:Cloud Firestore访问磁贴。

You will access the Firestore page of the project and get a prompt to create a Database in a big banner:

您将访问该项目的Firestore页面,并提示创建大横幅广告的数据库:

Image for post
Image 20: Firestore creation of the first database.
图20:第一个数据库的Firestore创建。

Click on the Create database button and a modal screen will appear. It will first ask you about database security. For development and this tutorial, you will set it to “test mode,” but take into account that for a production app, it is important to enable “production mode” because you want your users’ data to be safe.

单击创建数据库按钮,将出现一个模态屏幕。 它将首先询问您有关数据库安全性的信息。 对于开发和本教程,您将其设置为“测试模式”,但要考虑到生产应用程序,启用“生产模式”很重要,因为您希望用户数据安全。

Image for post
Image 21: Firestore database modes.
图像21:Firestore数据库模式。

Click Next and you will be asked for the location of the server. Usually, it is recommended to choose the location closest to you or your users, as this will reduce the loading times. I think the Firebase Console automatically suggests the one closest to you.

单击下一步,系统将询问您服务器的位置。 通常,建议选择距离您或您的用户最近的位置,因为这样可以减少加载时间。 我认为Firebase控制台会自动建议您附近的一个。

Image for post
Image 22: Firestore database location.
图片22:Firestore数据库位置。

Once you click on Done, it will show you a Provisioning Cloud Firestore spinner (Image 23). I was once told in a Google Cloud conference that this provisioning may take up to three minutes if the service is too saturated, but it usually takes less than that.

单击“完成”后,它将向您显示Provisioning Cloud Firestore微调框(图23)。 我曾在一次Google Cloud会议上被告知,如果服务过于饱和,此设置可能需要三分钟的时间,但通常花费的时间少于该时间。

Image for post
Image 23: Firestore provisioning spinner.
图片23:Firestore置备微调框。

配置数据库 (Configure the Database)

Once the provisioning process is completed, you will see your database like in the image below. The database is empty and waiting for you to start working on it:

设置过程完成后,您将在下图中看到数据库。 数据库为空,等待您开始使用它:

Image for post
Image 24: Firestore database empty.
图片24:Firestore数据库为空。

Click on the “+ start collection” text to create your first collection. The database has a multi-column structure based on collections > documents > fields. According to the demo app, we want our collection to be called MyGroceries, as this will contain all our groceries.

单击“ +开始收集”文本以创建您的第一个收集。 该数据库具有基于集合>文档>字段的多列结构。 根据演示应用程序,我们希望将我们的收藏集命名为MyGroceries,因为它将包含我们所有的杂货。

Image for post
Image 25: Firestore adding a collection.
图片25:Firestore添加了一个集合。

Then for the document, you will also be prompted for a name. It can be “TodaysGroceries” or the date (e.g. “March30–2020”) in case you want to have several lists in your app.

然后,对于文档,还将提示您输入名称。 如果您想在应用中包含多个列表,则可以是“ TodaysGroceries”或日期(例如“ March30–2020”)。

Add the fields with sample data:

添加带有示例数据的字段:

  1. Item1 = string — banana

    Item1 =字符串-香蕉
  2. Item2 = string — peanut butter

    Item2 =细绳—花生酱
  3. Item3 = string — bread

    Item3 =字符串—面包

Add as many as you want or just copy the sample data we have in the app.

根据需要添加任意数量,或仅复制我们在应用程序中拥有的示例数据。

Image for post
Image 26: Firestore adding a document.
图片26:Firestore添加文档。

Once you have added all the fields, your database should look like Image 27 below:

添加所有字段后,数据库应如下图27所示:

Image for post
Image 27: Firestore database with demo data.
图片27:具有演示数据的Firestore数据库。

结论 (Conclusion)

That’s it! We now have Firebase Console and the Android app configured to use Firestore with a database in it. In the next part, we will be integrating this database to Android Architecture Components. If you want to see how the project should work in the end, you can download the finished project on my website.

而已! 现在,我们将Firebase控制台和Android应用程序配置为将Firestore与其中的数据库一起使用。 在下一部分中,我们将将此数据库集成到Android Architecture Components中。 如果您想查看项目的最终效果,可以在我的网站上下载完成的项目。

If you have any doubts don’t hesitate to leave a comment and I will reply as soon as possible.

如有任何疑问,请随时发表评论,我会尽快答复。

See you next time!

下次见!

翻译自: https://medium.com/better-programming/how-to-set-up-a-database-with-firebase-firestore-to-use-with-android-architecture-components-992ab5df8223

firebase创建数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值