Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

関数の呼び出し - Java SDK

項目一覧

  • 名前を使用して関数を呼び出す

このセクションの例では、2 つの引数を受け取り、それらを加算して結果を返す sumという名前の単純な関数を呼び出す方法が示されています。

// sum: adds two numbers
exports = function(a, b) {
return a + b;
};

SDK から関数を実行するには、 アプリ の getFunctions() メソッドを使用して 関数マネージャー を取得します。呼び出す関数の名前とパラメータをCallFunction()またはCallFunctionAsync() に渡します。

String appID = YOUR_APP_ID; // replace this with your App ID
App app = new App(new AppConfiguration.Builder(appID).build());
Credentials credentials = Credentials.anonymous();
app.loginAsync(credentials, it -> {
if (it.isSuccess()) {
User user = app.currentUser();
assert user != null;
Functions functionsManager = app.getFunctions(user);
List<Integer> args = Arrays.asList(1, 2);
functionsManager.callFunctionAsync("sum", args, Integer.class, result -> {
if (result.isSuccess()) {
Log.v("EXAMPLE", "Sum value: " + result.get());
} else {
Log.e("EXAMPLE", "failed to call sum function with: " + result.getError());
}
});
} else {
Log.e("EXAMPLE", "Error logging into the Realm app. Make sure that anonymous authentication is enabled. Error: " + it.getError());
}
});
val appID = YOUR_APP_ID // replace this with your App ID
val app: App = App(AppConfiguration.Builder(appID).build())
val anonymousCredentials: Credentials = Credentials.anonymous()
app.loginAsync(anonymousCredentials) {
if (it.isSuccess) {
val user: User? = app.currentUser()
val functionsManager: Functions = app.getFunctions(user)
val args: List<Int> = listOf(1, 2)
functionsManager.callFunctionAsync("sum", args, Integer::class.java) { result ->
if (result.isSuccess) {
Log.v("EXAMPLE", "Sum value: ${result.get()}")
} else {
Log.e("EXAMPLE", "failed to call sum function with: " + result.error)
}
}
} else {
Log.e("EXAMPLE", "Error logging into the Realm app. Make sure that anonymous authentication is enabled. Error: " + it.error)
}
}

戻る

App Services Appへの接続