JSONStore コレクションのデータの Cloudant DB への同期
improve this page | report issueReact Native 開発環境のセットアップ
React Native 開発用にご使用のマシンをセットアップするには、React Native の『Gettings Started』ページに記載されている手順に従います。
React Native アプリケーションへの JSONStore SDK の追加
React Native 用の JSONStore SDK は、npm から React Native モジュールとして入手可能です。
新規 React Native プロジェクトの開始
- 新規 React Native プロジェクトを作成します。
react-native init MyReactApp
- MobileFirst SDK をアプリケーションに追加します。
cd MyReactApp npm install react-native-ibm-mobilefirst-jsonstore --save
- すべてのネイティブ依存関係をアプリケーションにリンクします。
react-native link
JSONStore コレクションのデータの Cloudant DB への同期
すべてのアプリケーション・データをローカルに配置することの欠点は、アプリケーションをアンインストールするとデータが失われることです。 この問題に対処するために、IBM JSONStore は Cloudant DB との同期機能を提供しています。
import { JSONStoreCollection, WLJSONStore, JSONStoreInitOptions, JSONStoreSyncPolicy, JSONStoreAddOptions } from 'react-native-ibm-mobilefirst-jsonstore';
JSONStore コレクションからデータを同期するためのステップは以下の 2 つです。
-
コレクションのオープン。通常の
JSONStoreCollection
と同期されたJSONStoreCollection
との唯一の違いはオープンの方法です。 同期された JSONStoreCollections は対応するJSONStoreInitOptions
を使用してオープンします。 JSONStoreInitOptions では、同期ポリシーと、データを同期するアダプターを決定します。 このアダプターは、基本的には Cloudant Sync アダプターです。詳しくは、ここを参照してください。 JSONStoreInitOptions は APIsetSyncOptions(syncPolicy, adapterName)
を提供します。 JSONStoreSyncPolicy は、「SYNC_NONE」、「SYNC_DOWNSTREAM」、「SYNC_UPSTREAM」のいずれかの値にする必要があります。 adapterName は、Cloudant DB で機能する、MobileFirst Server にデプロイされているアダプターの名前です。 Sync を機能させるには、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); });
-
Sync API の呼び出し。
openCollection()
API が成功すると、Sync を使用してオープンしたすべての JSONStoreCollections が同期を自動的にトリガーします。
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.