Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

同期された Realm を開く - Flutter SDK

このページでは、Device Sync を使用して同期された Realm を開く方法について説明します。 同期されていない Realm を開いて構成する方法については、「 Realm のオープンと閉じ 」を参照してください。

Flutter アプリケーションで Flexible Sync を使用して Realm を開く前に、

  1. バックエンド で Flexible Sync を構成します。 Flexible Sync は、クライアント アプリケーションで使用する前に、バックエンドで構成する必要があります。

  2. アプリ クライアントを初期化します。

  3. クライアント プロジェクトでユーザーを認証します。

同期された邦土を開くには、ログイン ユーザー、 Realm オブジェクトスキーマのリスト、および追加の任意の名前付き引数を Configuration.FlexibleSync() コンストラクターに渡します。このコンストラクターは FlexibleSyncConfiguration を返します。次に、FlexibleSyncConfiguration をRealm ()に渡して、 邦土のインスタンスを開きます。 邦土を開くと、バックグラウンドで App Services とデータが同期されます。

final currentUser = await app.logIn(credentials);
final config = Configuration.flexibleSync(currentUser, [Tricycle.schema],
path: 'flex.realm');
final realm = Realm(config);

同期された Realm を開き、同期サブスクリプションを構成および管理します。

邦土を開くときにすべてのデータを App Services と同期するには、非同期メソッドRealm.open() を使用します。この操作は、 邦土を返す前に、利用可能なすべてのデータを同期します。

最初に開くと、 Realm.open()は同期サブスクリプションに一致するすべてのデータをダウンロードします。 その後を開くと、最新の変更のみがダウンロードされます。 デバイスが同期していない間にデータセットに初期 Realm サイズと の更新によっては、最初に開いたパフォーマンスは遅く、その後を開くと高速になる可能性があります。

クライアント アプリケーションがオフラインの場合、 Realm.open()は解決されません。 Realm.open()を使用する前に、デバイスがインターネットに接続されているかどうかを確認する必要があります。 デバイスがインターネットに接続されていない場合でも、インターネット接続が利用可能であれば、 Realm()を使用してRealmをすぐに開き、バックグラウンドでデータを同期できます。

// Helper function to check if device is connected to the internet.
Future<bool> isDeviceOnline() async {
// ...logic to check if device is online
}
final config = Configuration.flexibleSync(currentUser, [Tricycle.schema]);
// Only use asynchronous open if app is online.
late Realm realm;
if (await isDeviceOnline()) {
// If the device is online, download changes and then open the realm.
realm = await Realm.open(config);
} else {
// If the device is offline, open the realm immediately
// and automatically sync changes in the background when the device is online.
realm = Realm(config);
}

同期の状態を追跡するには、プログレス コールバック を任意の名前付き引数 onProgressCallback に渡します。

double progressEstimate = -1;
final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
progressEstimate = syncProgress.progressEstimate;
print('Sync progress: ${progressEstimate * 100}% complete.');
if (progressEstimate == 1.0) {
// Transfer is complete
}
});

Tip

Realm を開いた後に進捗通知を構成する場合は、 SyncSession.getProgressStream を使用します。

進行中の同期をキャンセルするには、CancelTokenインスタンスを任意の名前付き引数 cancellationToken に渡します。同期をキャンセルするには、 CancelToken.cancel() を呼び出します。

final token = CancellationToken();
// Cancel the open operation after 30 seconds.
// Alternatively, you could display a loading dialog and bind the cancellation
// to a button the user can click to stop the wait.
Future<void>.delayed(
const Duration(seconds: 30),
() => token.cancel(CancelledException(
cancellationReason: "Realm took too long to open")));
// If realm does not open after 30 seconds with asynchronous Realm.open(),
// open realm immediately with Realm() and try to sync data in the background.
late Realm realm;
try {
realm = await Realm.open(config, cancellationToken: token);
} on CancelledException catch (err) {
print(err.cancellationReason); // prints "Realm took too long to open"
realm = Realm(config);
}

Realm() と Realm.open()

このセクションでは、アプリケーションで Realm を開くためにRealm()Realm.open()を使用するシナリオの例を比較します。

Consider an app that allows users to record and save their favorite recipes. アップデートをダウンロードするのを待たずに、またはオフラインの場合でも、新しいレシピを作成するオプションをユーザーに付与したい場合があります。 この場合はRealm()が推奨されます。 ユーザーはオフラインで操作できますが、アプリは次にネットワーク接続を確保したときにレシピを同期します。

テーブルと電話のバージョンがあるゲームアプリを考えてみましょう。 ユーザーはテーブルと電話の両方でゲームをプレイします。 ユーザーはテーブルで 3 つのレベルに進みます。 その後、ユーザーは電話でゲームを開きます。 この場合、Realm を開くには、 Realm.open()を使用するのがより優れた方法です。 Realm.open()は Realm を返す前にデータを同期するため、最初の読み込み時間が遅くなる場合でも、ユーザーの進行状況がアプリの使用を開始する前にアプリで同期されるようになります。

一般的な Realm 構成オプションの詳細については、「 Realm の構成 」を参照してください。

追加の構成プロパティを使用して同期された Realm のエラーを処理するには、「 同期エラーの処理 」を参照してください。

同期された Realm の操作が終了したら、メモリ リークを防ぐために同期された Realm を閉じます。

realm.close();
  • 同期サブスクリプションの管理: 同期済み Realm を開いてから 同期サブスクリプション を追加、変更、削除する方法を学びます。

  • 同期セッションの管理: 同期の一時停止と再開、アップロードとダウンロードの進行状況の監視、ネットワーク接続のチェックなど、同期セッションの状態を管理する方法を学習します。

  • 複数のプロセスからのデータを同期 : 1 つの Realm を使用して複数のプロセスのデータを同期する方法を学びます。

戻る

アプリへの同期の追加

項目一覧