JSONStore 콜렉션의 데이터를 Cloudant DB에 동기화
improve this page | report issueReact Native 개발 환경 설정
React Native 개발을 위해 사용하는 시스템을 설정하려면 React Native 시작하기 페이지에서 제공되는 지시사항을 따르십시오.
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와의 SYNC 기능을 제공합니다.
import { JSONStoreCollection, WLJSONStore, JSONStoreInitOptions, JSONStoreSyncPolicy, JSONStoreAddOptions } from 'react-native-ibm-mobilefirst-jsonstore';
다음 두 단계를 통해 JSONStore 콜렉션의 데이터를 동기화할 수 있습니다.
-
콜렉션 열기. 보통의
JSONStoreCollection
과 동기화된JSONStoreCollection
의 유일한 차이점은 콜렉션이 열리는 방법에 있습니다. 동기화된 JSONStoreCollection은 해당JSONStoreInitOptions
로 열립니다. JSONStoreInitOptions는 동기화 정책과 데이터를 동기화할 어댑터를 결정하는 곳입니다. 이 어댑터는 기본적으로 Cloudant 동기화 어댑터입니다. 추가 정보는 여기를 참조하십시오. JSONStoreInitOptions는 APIsetSyncOptions(syncPolicy, adapterName)
를 제공합니다. JSONStoreSyncPolicy는 다음 값 중 하나여야 합니다. [‘SYNC_NONE’, ‘SYNC_DOWNSTREAM’, ‘SYNC_UPSTREAM’]. adapterName은 MobileFirst Server서버에 배치되고 Cloudant DB와 연동하는 어댑터의 이름입니다. 동기화가 작동하려면 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); });
-
동기화 API를 호출하면 동기화로 열린 모든 JSONStoreCollections가
openCollection()
API 성공 시 동기화를 자동으로 트리거합니다.
JSONStoreCollection이 JSONStoreSyncPolicy.SYNC_DOWNSTREAM 정책으로 열리는 경우sync()
API를 명시적으로 호출하여 최신 풀을 페치할 수 있습니다.
JSONStoreCollection이 JSONStoreSyncPolicy.SYNC_UPSTREAM 정책으로 열리는 경우, 콜렉션에서 문서를 추가, 업데이트 또는 제거하면 동기화 프로세스가 자동으로 트리거됩니다.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.