Android
Real Time
Android 中的实时查询实现与 Back4App 集成指南
10 分
使用实时查询的实时应用程序 介绍 实时查询旨在用于 实时反应式应用程序 ,在这些应用中,仅使用传统查询范式会带来一些问题,例如响应时间增加以及高网络和服务器使用率。实时查询应在需要持续更新页面以获取来自数据库的新数据的情况下使用,这种情况通常发生在在线游戏、消息客户端和共享待办事项列表中,但不限于此。 本节解释如何通过 https //www back4app com/ 在 android 环境中使用 back4app 的实时查询。 本教程使用在 android studio arctic fox 2020 3 1 patch 1 中创建的基本应用程序,使用 compilesdk 30 compilesdk 30 , minsdk 21 minsdk 21 和 targetsdk 30 targetsdk 30 您可以随时通过我们的 github 仓库访问完整项目。 https //github com/templates back4app/android crud operations kotlin https //github com/templates back4app/android crud operations java 目标 以下是我们将要实现的预览: 先决条件 要完成本教程,我们需要: https //developer android com/studio/index html 在 back4app 上创建的应用程序。 注意: 请遵循 https //www back4app com/docs/get started/new parse app 以了解如何在 back4app 上创建 parse 应用程序。 一个连接到 back4app 的安卓应用程序。 注意: 请遵循 https //www back4app com/docs/android/parse android sdk 以创建一个连接到 back4app 的 android studio 项目。 一台运行 android 4 1(果冻豆)或更高版本的设备(或 https //developer android com/studio/run/managing avds html )。 1 启用实时查询 在开始编码之前,必须在数据库中有一个类以启用实时查询。为此,只需在 https //www back4app com/ 上找到您的应用程序,然后单击 仪表板 仪表板 > 创建一个类 创建一个类 ,如下所示: 现在,要启用实时查询功能,请登录到您的帐户,访问 https //www back4app com/ ,找到您的应用程序并点击 服务器设置 服务器设置 ,然后找到“服务器 url 和实时查询”块并点击 设置 设置 然后,您将到达如下所示的页面。在此页面上,您需要勾选 激活您的 back4app 子域名 激活您的 back4app 子域名 选项, 激活实时查询 激活实时查询 选项以及您希望激活实时查询的所有类,如下所示 激活 webhosting 是使用实时查询的必要条件,因为您的域名将作为实时服务器工作。 注意 要了解有关 webhosting 的更多信息,请查看 https //www back4app com/docs/platform/activating web hosting 2 设置 livequery 客户端 https //github com/parse community 有一个 live query 客户端的实现,适用于 https //github com/parse community/parselivequery android 。有必要实现官方的 live query 客户端,它运行良好。为此,请在您的应用程序 app/build gradle app/build gradle 文件的依赖项部分添加以下行,并同步您的项目: app/build gradle 1 dependencies { 2 3 // don't forget to change the line below with the latest version of parse sdk for android 4 implementation "com github parse community parse sdk android\ parse 1 26 0" 5 implementation 'com github parse community\ parselivequery android 1 2 2' 6 7 } 在这个项目中,我们还将创建一个名为 message message 的类,它将包含我们的消息。 3 订阅您的查询 要开始使用 live queries,首先创建一个 livequeryclient livequeryclient ,它将为您管理 websocket 连接。为此,您需要提供应用程序 id、javascript 密钥以及您在第一步中设置的 live query 服务器 url。 1 parse initialize(new parse configuration builder(this) 2 applicationid(getstring(r string back4app app id)) 3 clientkey(getstring(r string back4app client key)) 4 server(getstring(r string back4app server url)) 5 build());1 parse initialize(parse configuration builder(this) 2 applicationid(getstring(r string back4app app id)) 3 clientkey(getstring(r string back4app client key)) 4 server(getstring(r string back4app server url)) 5 build()) 初始化的代码 livequeryclient livequeryclient 如下所示: 1 parselivequeryclient parselivequeryclient = parselivequeryclient factory getclient();1 val parselivequeryclient = parselivequeryclient factory getclient() 我们有一个名为 messageadapter messageadapter messageadapter 函数在对象添加、删除或更新时触发。在这里我们的 messageadapter messageadapter 函数 1 public void additem(parseobject t) { 2 this list add(t); 3 notifyiteminserted(list size() 1); 4 } 5 6 public void removeitem(parseobject object) { 7 for (int i = 0; i < list size(); i++) { 8 if (list get(i) getobjectid() equals(object getobjectid())){ 9 list remove(i); 10 notifyitemremoved(i); 11 notifyitemrangechanged(i, list size()); 12 return; 13 } 14 } 15 } 16 public void updateitem(parseobject object) { 17 for (int i = 0; i < list size(); i++) { 18 if (list get(i) getobjectid() equals(object getobjectid())){ 19 list set(i,object); 20 notifydatasetchanged(); 21 return; 22 } 23 } 24 }1 fun additem(t parseobject?) { 2 list!! add(t!!) 3 notifydatasetchanged() 4 } 5 6 fun removeitem(`object` parseobject) { 7 for (i in list!! indices) { 8 if (list!!\[i] objectid == `object` objectid) { 9 list!! removeat(i) 10 notifyitemremoved(i) 11 notifyitemrangechanged(i, list!! size) 12 return 13 } 14 } 15 } 16 17 fun updateitem(`object` parseobject) { 18 for (i in list!! indices) { 19 if (list!!\[i] objectid == `object` objectid) { 20 list!!\[i] = `object` 21 notifydatasetchanged() 22 return 23 } 24 } 25 } 然后,您应该创建一个 parsequery parsequery 来订阅您想要的对象类型。订阅是一个事件发射器,当满足您查询的对象发生变化时,它将触发事件。 在这个例子中,您将进行一个基本查询,并订阅对 message message 对象所做的所有更改 有关查询和订阅的更多信息,请访问 http //docs parseplatform org/android/guide/#queries 1 parsequery\<parseobject> parsequery = new parsequery<>("message"); 2 subscriptionhandling = parselivequeryclient subscribe(parsequery); 3 subscriptionhandling handlesubscribe(q > { 4 subscriptionhandling handleevent(subscriptionhandling event create, (query, object) > { 5 mainactivity this runonuithread(() > { 6 messagesadapter additem(object); 7 }); 8 }); 9 subscriptionhandling handleevent(subscriptionhandling event delete, (query, object) > { 10 mainactivity this runonuithread(() > { 11 messagesadapter removeitem(object); 12 }); 13 }); 14 subscriptionhandling handleevent(subscriptionhandling event update, (query, object) > { 15 mainactivity this runonuithread(() > { 16 messagesadapter updateitem(object); 17 }); 18 }); 19 });1 val parsequery = parsequery\<parseobject>("message") 2 subscriptionhandling = parselivequeryclient!! subscribe(parsequery) 3 subscriptionhandling!! handlesubscribe { subscriptionhandling!! handleevent(subscriptionhandling event create 4 ) { parsequery\<parseobject?>?, `object` parseobject? > 5 runonuithread { messagesadapter!! additem(`object`) } 6 } 7 subscriptionhandling!! handleevent(subscriptionhandling event delete 8 ) { parsequery\<parseobject?>?, `object` parseobject? > 9 runonuithread { messagesadapter!! removeitem(`object`!!) } 10 } 11 subscriptionhandling!! handleevent(subscriptionhandling event update 12 ) { parsequery\<parseobject?>?, `object` parseobject? > 13 runonuithread { messagesadapter!! updateitem(`object`!!) } 14 } 15 } 注意: 我们在应用程序中触发了所有这些事件。您还可以从 back4app(从表或 javascript 控制台)触发这些创建、更新和删除事件。 完成了! 在这一点上,您已经掌握了如何使用实时查询在 android 环境中创建实时反应式应用程序的知识,以及如何使用 back4app 设置实时查询。现在您可以开始在自己的应用程序中实现它。 您现在可以开始探索 https //www back4app com/product/parse server 和 https //www back4app com/product/addons