Cloud Code Functions
For complex apps, sometimes you need a bit of logic that isn’t running on the mobile device. Cloud Code makes it possible.
Cloud Code is built on the same JavaScript SDK that powers thousands of apps. The only difference is that this code runs in your Parse Server rather than running on the user’s mobile device. When you update the Cloud Code, it becomes available to all mobile environments instantly and you don’t have to wait until a new release of your application comes up. This lets you change app behavior on the fly and also lets you add new features on your app faster.
This section explains how to create and deploy Cloud Code, followed by how to call a cloud function in Android projects through Back4App.
Even if you’re only familiar with mobile development, we hope you’ll find Cloud Code straightforward and easy to use.
You can find more in-depth information in Parse Official Cloud Code Documentation.
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
Create a new file and name it main.js and add the following Parse.Cloud.define function, which has its name and a callback as arguments.
You can pass parameters to your Cloud function from your Android App and access then within the request.params object.
- Find the Cloud Code and click on Functions & Web Hosting. It looks like this:
3. Upload or create a new file (you can also edit the current main.js file directly on the browser). Then, click at Deploy as shown here:
Import the following dependecies:
To call your Cloud Code function, you need to call a special android function: ParseCloud.callFunctionInBackground. Its first parameter is the function name on Cloud Code and the second one is the HashMap that has every parameter that will be passed to the function. The third argument is the callback that will be executed after the function has been called.
The following code calls the function:
In this function, the mapObject has a key called answer, which contains the value hello world, which will be printed on the screen by the Toast class when the code is executed.
At this stage, you are able to code and call your own Cloud Code in your Android App using Parse Server Core features through Back4App!