将 JSONStore 集合的数据同步到 Cloudant DB

improve this page | report issue


设置 React Native 开发环境

遵循 React Native 入门页面中提供的指示信息来设置您的机器进行 React Native 开发。

将 JSONStore SDK 添加到 React Native 应用程序

npm 中,JSONStore SDK for React Native 作为 React Native 模块提供。

开始使用新 React Native 项目

  1. 创建新 React Native 项目。
     react-native init MyReactApp
    
  2. 将 MobileFirst SDK 添加到您的应用程序。
     cd MyReactApp
     npm install react-native-ibm-mobilefirst-jsonstore --save
    
  3. 将所有本机依赖关系添加到您的应用程序。
    react-native link
    

将 JSONStore 集合的数据同步到 Cloudant DB

将所有应用程序数据保存到本地的弊端是卸载应用程序后您将丢失数据。为解决此问题,IBM JSONStore 随 Cloudant DB 提供同步功能。

import { JSONStoreCollection, WLJSONStore, JSONStoreInitOptions, JSONStoreSyncPolicy, JSONStoreAddOptions } from 'react-native-ibm-mobilefirst-jsonstore';

从 JSONStore 集合同步数据时需要执行两个步骤:

  1. 打开集合,常规 JSONStoreCollection 与同步的 JSONStoreCollection 之间的唯一差异是打开方式不同。使用对应的 JSONStoreInitOptions 打开同步的 JSONStoreCollections。您可以在 JSONStoreInitOptions 中确定同步策略以及要将数据与之同步的适配器。该适配器基本上是 Cloudant Sync Adapter,在此处了解更多信息。JSONStoreInitOptions 提供 API setSyncOptions(syncPolicy, adapterName)。JSONStoreSyncPolicy 需要为以下值之一:[‘SYNC_NONE’, ‘SYNC_DOWNSTREAM’, ‘SYNC_UPSTREAM’]。adapterName 是部署在使用 Cloudant DB 的 MobileFirst Server 上的适配器的名称。请确保正确输入 Cloudant DB 详细信息以实现同步。

     var initOptions = new JSONStoreInitOptions();
     initOptions.setSyncOptions(JSONStoreSyncPolicy.SYNC_UPSTREAM, "JSONStoreCloudantSync");
     var collection = new JSONStoreCollection('favourites');
     WLJSONStore.openCollections(['favourites'], initOptions).then(data => {	console.log("Successfully opened collection with Sync Policy!");
    }).catch(err => {	console.log(err);
    });
    
  2. 调用同步 API,通过“同步”打开的所有 JSONStoreCollections 将在 openCollection() API 成功运行时自动触发同步。
    如果通过 JSONStoreSyncPolicy.SYNC_DOWNSTREAM 策略打开 JSONStoreCollection,那么可以显式调用 sync() API 以访存最新的提取。
    如果通过 JSONStoreSyncPolicy.SYNC_UPSTREAM 策略打开 JSONStoreCollection,那么在通过集合添加、更新或移除文档时将自动触发同步过程。您仍可以调用 sync() API 以显式触发同步。

     var favCollection = new JsonStoreCollection('favourites');
     favCollection.sync();
    
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020