React Native 应用程序中的 JSONStore
improve this page | report issue
先决条件
- 阅读 JSONStore 父教程
- 确保已将 MobileFirst React Native 核心 SDK 添加到项目。遵循向 React-Native 应用程序添加 Mobile Foundation SDK 教程。
跳转至:
添加 JSONStore
要向 React Native 应用程序添加 JSONStore 插件:
- 打开命令行窗口并浏览至 React Native 项目文件夹。
- 运行以下命令:
npm install react-native-ibm-mobilefirst-jsonstore --save
基本用法
创建新 JSONStore 集合
- 我们使用
JSONStoreCollection
类创建 JSONStore 的实例。我们还可以将其他配置设置为此新创建的 JSONStore 集合(例如,设置搜索字段)。 - 要开始与现有的 JSONStore 集合交互(例如:添加或移除数据),我们需要打开集合。我们使用
openCollections()
API 来执行此操作。var collection = new JSONStoreCollection('people'); WLJSONStore.openCollections(['people']) .then(res => { // handle success }).catch(err => { // handle failure });
添加
使用 addData()
API 将 JSON 数据存储在集合中。
var data = { "name": "John", age: 28 };
var collection = new JSONStoreCollection('people');
collection.addData(data)
.then(res => {
// handle success
}).catch(err => {
// handle failure
});
您可以使用该 API 添加单个 JSON 对象或 JSON 对象数组。
查找
- 使用
find
来通过查询查找集合中的文档。 - 使用
findAllDocuments()
API 来检索集合中的所有文档。 - 使用
findDocumentById()
和findDocumentsById()
API 来根据文档唯一标识进行搜索。 - 使用
findDocuments()
API 来查询集合。对于查询,您可以使用JSONStoreQueryPart
类对象来过滤数据。
将
JSONStoreQueryPart
对象数组作为参数传递至findDocuments
API。
var collection = new JSONStoreCollection('people');
var query = new JSONStoreQueryPart();
query.addEqual("name", "John");
collection.findDocuments([query])
.then(res => {
// handle success
}).catch(err => {
// handle failure
});
除去
使用 remove
以删除集合中的文档。
var id = 1; // for example
var collection = new JSONStoreCollection('people');
collection.removeDocumentById(id)
.then(res => {
// handle success
}).catch(err => {
// handle failure
});
除去集合
使用 removeCollection
来删除集合中存储的所有文档。 此操作类似于数据库术语中的删除表。
var collection = new JSONStoreCollection('people');
collection.removeCollection()
.then(res => {
// handle success
}).catch(err => {
// handle failure
});
IBM MobileFirst JSONStore 的样本应用程序
在此处下载样本。
运行样本
在样本的根目录中,运行以下命令,这将安装所有项目依赖关系:
npm install
注:确保您的 mfpclient.properties 和 mfpclient.plist 指向正确的 MobileFirst Server。
- 注册应用程序。转至
android
目录并运行以下命令:mfpdev app register
- 配置应用程序。(仅针对 Android)
-
从 React Native 项目根目录打开
android/app/src/main/AndroidManifest.xml
文件。
向<manifest>
标记添加以下行:
xmlns:tools="http://schemas.android.com/tools"
向<application>
标记添加以下行:
tools:replace="android:allowBackup"
react-native-ibm-mobilefirst 库需要执行此步骤。 -
从 React Native 项目根目录打开
android/app/build.gradle
文件。
在 android {} 内添加以下代码:packagingOptions{ exclude 'META-INF/ASL2.0' }
react-native-ibm-mobilefirst-jsonstore 库需要执行此步骤。
-
- 运行应用程序。返回至根目录并浏览至
iOS
目录,然后运行以下命令:mfpdev app register
我们现在可运行应用程序。 要在 Android 上运行,请执行以下命令:
react-native run-android
Last modified on June 01, 2020