Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

Atlas へのデータのストリーム - React Native SDK

項目一覧

  • クライアント アプリケーションから一方向にデータを同期
  • 非対称オブジェクトの定義
  • App Services Appによる接続と認証
  • Realm を開く
  • 非対称オブジェクトの作成

Data Ingestを使用して、クライアント アプリケーションから Flexible Sync が有効化されている Atlas App Services アプリにデータをストリーミングできます。

クラウドにデータを送信する気象センサーなど、IoT アプリケーションでデータを一方向に同期したい場合があります。 Data Ingest は、小売アプリからの請求書作成や、アプリケーション イベントのログ記録など、競合の解決を必要としない他のタイプの不変データを書き込む場合にも役立ちます。

Data Ingest は、クライアント側の挿入専用ワークロードのパフォーマンスを向上させるために最適化されています。

1

非対称オブジェクトはデータを一方向に同期します。 オブジェクトモデル asymmetrictrueに設定して非対称オブジェクトを定義します。

class WeatherSensor extends Realm.Object {
static schema = {
name: 'WeatherSensor',
// sync WeatherSensor objects one way from your device
// to your Atlas database.
asymmetric: true,
primaryKey: '_id',
properties: {
_id: 'objectId',
deviceId: 'string',
temperatureInFahrenheit: 'int',
barometricPressureInHg: 'float',
windSpeedInMph: 'float',
},
};
}
class WeatherSensor extends Realm.Object<WeatherSensor> {
_id!: Realm.BSON.ObjectId;
deviceId!: string;
temperatureInFahrenheit!: number;
barometricPressureInHg!: number;
windSpeedInMph!: number;
static schema: ObjectSchema = {
name: 'WeatherSensor',
// sync WeatherSensor objects one way from your device
// to your Atlas database.
asymmetric: true,
primaryKey: '_id',
properties: {
_id: 'objectId',
deviceId: 'string',
temperatureInFahrenheit: 'int',
barometricPressureInHg: 'float',
windSpeedInMph: 'float',
},
};
}

非対称オブジェクトを定義する方法の詳細については、「 非対称オブジェクトの定義 」を参照してください。

2

クライアントからバックエンド アプリにデータをストリーミングするには、App Services バックエンドに接続し、ユーザーを認証する必要があります。

function LogIn() {
const app = useApp();
useEffect(() => {
app.logIn(Realm.Credentials.anonymous());
}, []);
return <></>;
}

Data Ingest は Flexible Sync の機能であるため、接続するアプリはFlexible Sync を使用する必要があります。

3

認証されたユーザーが作成されたら、Flexible Sync構成オブジェクトを使用して同期された Realm を開くことができます。

// Create a configuration object
const realmConfig = {schema: [WeatherSensor]};
// Create a realm context
const {RealmProvider, useRealm, useObject, useQuery} =
createRealmContext(realmConfig);
// Expose a sync realm
function AppWrapperSync() {
return (
<AppProvider id={APP_ID}>
<UserProvider fallback={LogIn}>
<RealmProvider sync={{flexible: true}}>
<App />
</RealmProvider>
</UserProvider>
</AppProvider>
);
}
// Create a configuration object
const realmConfig: Realm.Configuration = {
schema: [WeatherSensor],
};
// Create a realm context
const {RealmProvider, useRealm} = createRealmContext(realmConfig);
// Expose a sync realm
function AppWrapperSync() {
return (
<AppProvider id={APP_ID}>
<UserProvider fallback={LogIn}>
<RealmProvider sync={{flexible: true}}>
<App />
</RealmProvider>
</UserProvider>
</AppProvider>
);
}

双方向同期とは異なり、Data Ingest はFlexible Sync サブスクライブを使用しません。

非対称オブジェクトをクエリしたり、ローカル Realm に書き込んだりすることはできないため、非対称オブジェクトは双方向 Flexible Sync、パーティションベースの同期、またはローカル Realm の使用と互換性がありません。

4

Realm が開いたら、 Realm.create()を使用して書込みトランザクション内に非対称オブジェクトを作成できます。 非対称オブジェクトを作成する場合、 Realm.create()はオブジェクト自体ではなくundefinedを返します。

const App = () => {
// Getting access to our opened realm instance
const realm = useRealm();
const handleAddSensor = () => {
realm.write(() => {
realm.create('WeatherSensor', {
_id: weatherSensorPrimaryKey,
deviceId: 'WX1278UIT',
temperatureInFahrenheit: 66.7,
barometricPressureInHg: 29.65,
windSpeedInMph: 2,
});
});
};
return (
<Button
title='Add A New Sensor'
onPress={() => handleAddSensor()}
/>
);
};

これらのオブジェクトは読み取れません。 作成が完了すると、App Services バックエンドとリンクされた Atlas データベースに同期されます。

Atlas Device Sync は、このデータのライフサイクルを完全に管理します。 Data Ingest 同期が完了するまでデバイス上に保持され、その後デバイスから削除されます。

戻る

クライアント ログ レベルの設定