调用函数 - Java SDK
在此页面上
Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
本节中的示例演示如何调用一个名为 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) } }