Parse Database

Parse Platform

Help & Communication
Our preferred channels of communication for help, issues and disscussion.
Parse Server and Dashboard
The REST server and dashboard to manage your data.
Documentation
Learn more about deploying your own Parse Server, or dive into our detailed client SDK guides.
SDKs and Libraries
The open source versions of our SDKs with relevant links to learn more.
Parse Server Adapters
Official adapters for Parse Server.
Other Helpful Repos
A list of useful tools, libraries, and miscellaneous goodies to help you.
Building Data driven Apps with Parse - CodePath Android ...

Building Data driven Apps with Parse – CodePath Android …

Parse is an open-source platform that provides one of the easiest ways to get a database and RESTful API up and running. If you want to build a mobile app and don’t want to code the back-end by hand, give Parse a try.
Parse on back4app
We can deploy our own Parse data store and push notifications systems to back4app leveraging the server open-sourced by Parse.
To get started setting up our own Parse backend, check out our Configuring a Parse Server guide. Once the Parse server is configured, we can initialize Parse within our Android app pointing the client to our self-hosted URL. After that, the functions demonstrated in this guide work the same as they did before.
Alternatives to Parse
A comprehensive list of alternatives can be reviewed here. One of the primary alternatives is Google’s Firebase, which provides a hosted solution for analytics, crash reporting, and a realtime JSON database. One major difference is that Parse still provides many powerful constructs for querying data, whereas Firebase requires you to perform this querying based on child/parent relations. See this guide for more information on porting Parse applications to Firebase.
What is Parse?
Parse is an open-source Android SDK and back-end solution that enables developers to build mobile apps with shared data quickly and without writing any back-end code or custom APIs.
Parse is a application that is deployed onto a host such as Heroku (or AWS) and then creates an automatic API for user authentication and storing data to a MongoDB document store. Parse has the following features included by combining the mobile SDK and back-end service:
User registration and authentication
Connecting user with Facebook to create a user account.
Creating, querying, modifying and deleting arbitrary data models
Makes sending push notifications easier
Uploading files to a server for access across clients
In short, Parse makes building mobile app ideas much easier!
Setup
Installing the Parse SDK
Setting up Parse starts with deploying your own Parse instance to back4app or another app hosting provider.
Add this in your root file (not your module file):
allprojects {
repositories {…
maven { url “}}}
Open the app/ in your project and add the following dependencies:
dependencies {
implementation ”
implementation ‘com. squareup. ok3:logging-interceptor:3. 8. 1’ // for logging API calls to LogCat}
Notice that the version of Parse-SDK-Android was 1. 25. 0 when this documentation was released. Check the latest version.
Select Tools -> Android -> Sync Project with Gradle Files to load the libraries through Gradle. When you sync, it will import everything automatically. You can see the imported files in the External Libraries folder.
⚠️ You may see multiple errors regarding “Duplicate class pport… “.
If you see this error, you will need to add android. enableJetifier=true to your operties file, then click Sync Now. See sample in the screenshot below…
After clicking Sync Now and re-running the app, this error should be resolved.
Initializing the SDK
Next, we need to create an Application class and initialize Parse.
The following section walks you through the process. You can also go to the API reference page and Getting Started section to follow the process.
Be sure to replace the initialization line below with your correct Parse keys:
public class ParseApplication extends Application {
@Override
public void onCreate() {
super. onCreate();
// Use for troubleshooting — remove this line for production
tLogLevel(Parse. LOG_LEVEL_DEBUG);
// Use for monitoring Parse OkHttp traffic
// Can be, Level. HEADERS, or
// See to see the options.
er builder = new er();
HttpLoggingInterceptor LoggingInterceptor = new HttpLoggingInterceptor();
tLevel();
tworkInterceptors()(LoggingInterceptor);
// set applicationId, and server server based on the values in the back4app settings.
// any network interceptors must be added with the Configuration Builder given this syntax
itialize(new er(this). applicationId(“myAppId”) // should correspond to Application Id env variable. clientKey(“yourClientKey”) // should correspond to Client key env variable
(“)());}}
class ParseApplication: Application() {
override fun onCreate() {
super. onCreate()
tLogLevel(Parse. LOG_LEVEL_DEBUG)
val builder = er()
val LoggingInterceptor = HttpLoggingInterceptor()
tLevel()
tworkInterceptors()(LoggingInterceptor)
itialize(er(this). applicationId(“myAppId”) // should correspond to Application ID env variable. clientKey(“yourClientKey) // should correspond to Client key env variable
We also need to make sure to set the application instance above as the android:name for the application within the This change in the manifest determines which application class is instantiated when the app is launched and also adding the application ID metadata tag:



android_name=”. ParseApplication”>


Setup network permissions
We also need to add a few important network permissions to the


Testing Parse Client
Assuming you have access to the Parse instance, you can test the SDK to verify that Parse is working with this application.
Let’s add the test code to ParseApplication as follows:
// Your initialization code from above here
itialize(… );
// New test creation of object below
ParseObject testObject = new ParseObject(“TestObject”);
(“foo”, “bar”);
veInBackground();}}
Run your app and a new object of class TestObject will be sent to the Parse Cloud and saved. See browsing Parse data for more information about how to check this data.
If needed in your application, you might also want to setup push notifications through Parse as well at this time.
You should be setup now! Follow the remaining documentation guide to understand how to leverage Parse for your entire backend.
Working with Users
At the core of many apps, there is a notion of user accounts that lets users access their information in a secure manner. Parse has a specialized ParseUser as a part of their SDK which handles this functionality. Be sure to check out the Users docs for a complete overview. See the API docs for ParseUser for more details.
User Signup
Creating a new user account is the process of constructing a ParseUser object and calling signUpInBackground:
// Create the ParseUser
ParseUser user = new ParseUser();
// Set core properties
tUsername(“joestevens”);
tPassword(“secret123”);
tEmail(“”);
// Set custom properties
(“phone”, “650-253-0000”);
// Invoke signUpInBackground
gnUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now. } else {
// Sign up didn’t succeed. Look at the ParseException
// to figure out what went wrong}}});
val user = ParseUser()
// with statement allows access properties of User without needing to repeat
// i. e. ername =
// tPassword
with(user) {
username = “joestevens”
setPassword(“secret123”)
email = “”
put(“phone”, “650-253-0000”)
signUpInBackground({e ->
// to figure out what went wrong}})}
This call will asynchronously create a new user in your Parse App. Before it does this, it checks to make sure that both the username and email are unique. See the signup up docs for more details.
User Session Login
We can allow a user to signin by calling logInInBackground and passing the user details:
ParseUser. logInInBackground(“joestevens”, “secret123”, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user! = null) {
// Hooray! The user is logged in. } else {
// Signup failed. Look at the ParseException to see what happened. }}});
ParseUser. logInInBackground(“joestevens”, “secret123”, ({ user, e ->
// Signup failed. }}))
If the credentials are correct, the ParseUser will be passed back accordingly. You can now access the cached current user for your application at any time in order to determine the session status:
ParseUser currentUser = tCurrentUser();
if (currentUser! = null) {
// do stuff with the user} else {
// show the signup or login screen}
User Logout
A user can be signed back out with:
();
ParseUser currentUser = tCurrentUser(); // this will now be null
()
val currentUser = tCurrentUser() // this will now be null
That’s the basics of what you need to work with users. See more details by checking out the User official docs.
You can also have a Facebook Login or Twitter Login for your users easily following the guides linked.
Querying Users
To query for users, you need to use the special user query:
ParseQuery query = tQuery();
query. whereGreaterThan(“age”, 20); // find adults
ndInBackground(new FindCallback() {
public void done(List objects, ParseException e) {
// The query was successful. } else {
// Something went wrong. }}});
See a list of query constraints here.
Working With Data Objects
Storing data on Parse is built around the ParseObject. Each ParseObject contains key-value pairs of JSON-compatible data. This data is schema-less, which means that you don’t need to specify ahead of time what keys exist on each ParseObject. Each ParseObject has a class name that you can use to distinguish different sorts of data. See the API docs for ParseObject for more details.
Creating Parse Models
When using Parse, the best practice is to create models that represent our data and that subclass ParseObject to allow for Parse persistence. Suppose we wanted to create a TodoItem model:
import;
@ParseClassName(“TodoItem”)
public class TodoItem extends ParseObject {
// Ensure that your subclass has a public default constructor
public static final String KEY_AUTHOR = “author”;
public ParseUser getAuthor() {
return getParseUser(KEY_AUTHOR);}
public void setAuthor(ParseUser author) {
put(KEY_AUTHOR, author);}}
import
class TodoItem: ParseObject() {
var author: ParseUser?
get() = getParseUser(“author”)
// putOrIgnore does a smart cast to Koltin Any after an if check
set(author) = putOrIgnore(“author”, author)}
We need to make sure to register this class with Parse before we call itialize:
// Register your parse models
gisterSubclass();
// Add your initialization code here
itialize(this, “7zBztvyG4hYQ9XghgfqYxfRcL3SMBYWAj0GUL”, “iZWhgJRu6yKm3iNMbTaguLcNCV3qedijWL”);}}
Now we can add fields and constructors to our TodoItem:
public TodoItem() {
super();}
// Add a constructor that contains core properties
public TodoItem(String body) {
super();
setBody(body);}
// Use getString and others to access fields
public String getBody() {
return getString(“body”);}
// Use put to modify field values
public void setBody(String value) {
put(“body”, value);}
// Get the user for this item
public ParseUser getUser() {
return getParseUser(“owner”);}
// Associate each item with a user
public void setOwner(ParseUser user) {
put(“owner”, user);}}
Notice that now our model has getBody, setBody as well as property methods for storing which user created the TodoItem.
Note: When creating Parse models, avoid creating unnecessary member instance variables and instead rely directly on getString-type methods to retrieve the values of database properties.
Saving or Updating Objects
Let’s suppose we wanted to save a TodoItem to the Parse database, create a new TodoItem, set the data attributes and then trigger a save with saveInBackground:
TodoItem todoItem = new TodoItem(“Do laundry”);
// Set the current user, assuming a user is signed in
tOwner(tCurrentUser());
// Immediately save the data asynchronously
veInBackground();
// or for a more robust offline save
// veEventually();
Note that there are two ways to save an object: saveInBackground which executes immediately and saveEventually which will store the update on the device and push to the server once internet access is available.
See the saving objects and updating docs docs for more details. Also, check out the relational data section.
Querying Objects
Objects By Id
If you have the objectId, you can retrieve the whole ParseObject using a ParseQuery:
// Specify which class to query
ParseQuery query = tQuery();
// Specify the object id
tInBackground(“aFuEsvjoHt”, new GetCallback() {
public void done(TodoItem item, ParseException e) {
// Access data using the `get` methods for the object
String body = tBody();
// Access special values that are built-in to each object
String objectId = tObjectId();
Date updatedAt = tUpdatedAt();
Date createdAt = tCreatedAt();
// Do whatever you want with the data…
keText(, body, Toast. LENGTH_SHORT)();} else {
// something went wrong}}});
See retrieving objects official docs for information on refreshing stale objects and more.
Objects By Query Conditions
The general pattern is to create a ParseQuery, put conditions on it, and then retrieve a List of matching ParseObjects using the findInBackground method with a FindCallback. For example, to find all items created by a particular user:
// Define the class we would like to query
// Define our query conditions
query. whereEqualTo(“owner”, tCurrentUser());
// Execute the find asynchronously
ndInBackground(new FindCallback() {
public void done(List itemList, ParseException e) {
// Access the array of results here
String firstItemId = (0). getObjectId();
keText(, firstItemId, Toast. LENGTH_SHORT)();} else {
Log. d(“item”, “Error: ” + tMessage());}}});
See a list of query constraints here and check the queries overview for explanation of compound queries and relational queries.
Objects by Querying GeoLocation
Often we might want to query objects within a certain radius of a coordinate (for example to display them on a map). With Parse, querying by GeoPoint to retrieve objects within a certain distance of a location is built in. Check the AnyWall Tutorial and the whereWithinMiles and related where conditions for more details.
If you want to query this based on a map, first you can add a listener for the map camera. Next, you can determine the visible bounds of the map as shown there. Check the Maps Usage Guide for additional information on using the map.
Live Queries
One of the newer features of Parse is that you can monitor for live changes made to objects in your database To get started, make sure you have defined the ParseObjects that you want in your NodeJS server. Make sure to define a list of all the objects by declaring it in the liveQuery and classNames listing:
let api = new ParseServer({…,
// Make sure to define liveQuery AND classNames
liveQuery: {
// define your ParseObject names here
classNames: [‘Post’, ‘Comment’]}});
See this guide for more details. Parse Live Queries rely on the websocket protocol, which creates a bidirectional channel between the client and server and periodically exchange ping/pong frames to validate the connection is still alive. Websocket URLs are usually prefixed with ws or wss (secure) URLs. If you are deploying to a different server (Amazon), you may need to make sure that TCP port 80 or TCP port 443 are available.
You will need to also setup the client SDK by adding this dependency to your app/ config:
// add Parse dependencies too
implementation ”}
where X. X. X is the latest version
Next, instantiate the client:
ParseLiveQueryClient parseLiveQueryClient = tClient();
Define the query pattern you wish to listen for events:
ParseQuery query = tQuery();
Create a subscription to this ParseQuery instance:
SubscriptionHandling subscriptionHandling = bscribe(parseQuery)
Finally, listen to the events. You can listen for,,, and An enter and leave event reflects changes to an existing ParseObject that either now fulfill the criteria or no longer do so. See this guide for more information about the live queries protocol specification.
subscriptionHandling. handleEvent(, new SubscriptionHandling. HandleEventCallback() {
public void onEvent(ParseQuery query, Comment object) {
// HANDLING create event}})
Passing Objects Between Activities
Often with Android development, you need to pass an object from one Activity to another. This is done using the Intent system and passing objects as extras within a bundle. Fortunately, your subclasses of ParseObject are already Parcelable by default, since the ParseObject library implements Parcelable!
So, the simplest way to pass data between activities is simply to pass the entire Parse object through an Intent:
Intent i = new Intent(this, );
i. putExtra(“todo_object”, myTodoItem);
startActivity(i);
you can then use your Parse object within the child Activity:
TodoItem todoItem = getIntent(). getParcelableExtra(“todo_object”);
Log. i(“test”, tAuthor());
In your Java file for your model, be sure to add a public static final String to define the key for each property in your model. This helps the Parse library understand which column in your Parse Dashboard/database corresponds to each property in your model. An example would be the KEY_AUTHOR mentioned above in this article.
Associations
Objects can have relationships with other objects. To model this behavior, any ParseObject can be used as a value in other ParseObjects. Internally, the Parse framework will store the referred-to object in just one place, to maintain consistency.
One-to-One Relationships
For example, each Comment in a blogging app might correspond to one Post. To create a new Post with a single Comment, you could write:
@ParseClassName(“Post”)
public class Post extends ParseObject {
//… }
@ParseClassName(“Comment”)
public class Comment extends ParseObject {
//…
// Associate each comment with a user
put(“owner”, user);}
// Get the user for this comment
public ParseUser getOwner() {
// Associate each comment with a post
public void setPost(Post post) {
put(“post”, post);}
// Get the post for this item
public Post getPost() {
return (Post) getParseObject(“post”);}}
// Create the post
Post post = new Post(“Welcome Spring! “);
// Get the user
// Create the comment
Comment comment = new Comment(“Get milk and eggs”);
tPost(post);
tOwner(currentUser);
By default, when fetching an object, related ParseObjects are not fetched. We can preload (eagerly fetch these) by using the include method on any ParseQuery:
ParseQuery query = tQuery();
// Include the post data with each comment
clude(“owner”); // the key which the associated object was stored
// Execute query with eager-loaded owner
ndInBackground(new FindCallback() {…. }
We can eagerly load nested associations as well. If you have an objectA which has a column referencing objectB and then objectB has a column referencing objectC, you can get objectB and objectC by doing:
clude(“ObjectB. ObjectC”);
//… execute query…
Otherwise, these associated objects can only be retrieved once they have been fetched separately:
tCategory(). fetchIfNeededInBackground(new GetCallback() {
public void done(Category object, ParseException e) {
String title = tTitle();}});
Many-to-Many Relationships
You can also model a many-to-many relation using the ParseRelation object. This works similar to ArrayList, except that you don’t need to download all the ParseObjects in a relation at once.
@ParseClassName(“Tag”)
public class Tag extends ParseObject {
//.. is a tag to describe an item}
public ParseRelation getTagsRelation() {
return getRelation(“tags”);}
public void addTag(Tag tag) {
getTagsRelation()(tag);
saveInBackground();}
public void removeTag(Tag tag) {
saveInBackground();}}
By default, the list of objects in this relation are not downloaded. You can get the list of Posts by calling findInBackground on the ParseQuery returned by getQuery. The code would look like:
tTagsRelation(). getQuery(). findInBackground(new FindCallback() {
void done(List results, ParseException e) {
// results have all the Posts the current user liked. } else {
// There was an error}}});
For more details, check out the official Relational Data guide. For more complex many-to-many relationships, check out this official join tables guide when the many-to-many requires additional metadata.
Deleting Objects
To delete an object from the Parse Cloud:
leteInBackground();
Naturally we can also delete in an offline manner with:
leteEventually();
Local Storage Mode
Parse now supports a more powerful form of local data storage out of the box which can be used to store and retrieve ParseObjects, even when the network is unavailable. To enable this functionality, simply call Parse. enableLocalDatastore() before your call to initialize:
// Within the Android Application where Parse is initialized
Parse. enableLocalDatastore(this);
itialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
Saving To Local Store
You can store a ParseObject in the local datastore by pinning it. Pinning a ParseObject is recursive, just like saving, so any objects that are pointed to by the one you are pinning will also be pinned:
// Store object offline
InBackground();
Querying from Local Store
We can query from the local offline store with the fromLocalDatastore flag during any query operation:
// Flag indicates we will use offline store
omLocalDatastore();
//… }});
Further Details
For the full summary of how to utilize the offline mode for Parse, be sure to review the official local store guide in the Parse docs.
Using the Data Browser
Suppose we had a simple todo application with user accounts and items persisted to Parse. The next step is to setup and create our models using the Parse dashboard to manage your new app. Visit the “Data Browser” for the correct application and let’s create our User and TodoItem objects for our app.
First, remove the test code that we added previously and drop the “TestObject” listed in the browser to clear testing data.
Next, select “New Class” and pick “User” to create the user object used to manage session authetication:
Let’s also add our “Custom” class which can represent any custom data. In this case, we will create a TodoItem class:
Now, we need to add our custom columns to our class. In this case, let’s add a “body” field to our TodoItem by selecting “+Col” and then selecting the type as a String and column name as “body”:
Once you’ve finished adding your columns to the class, you can create as many additional classes as necessary and configure their respective columns. Let’s add a row of data to the class directly through the data browser:
We are now ready to access these classes within our application.
Troubleshooting
Check out our Troubleshooting Common Issues with Parse guide for a detailed look at common issues encountered and related solutions.
Additional Features
Parse has many powerful features in addition to the core functionality listed above.
Uploading Photos
Parse has full support for storing images and files uploaded by an application. Photos are stored using the ParseFile construct described in more detail here.
First, make sure to create a Parse model. Next, you will need to add a ParseFile object:
class Post extends ParseObject {
public ParseFile getMedia() {
return getParseFile(“media”);}
public void setMedia(ParseFile parseFile) {
put(“media”, parseFile);}}
class Post: ParseObject() {
var media: ParseFile?
get() = getParseFile(“media”)
set(file) {
put(“media”, file)}
A ParseFile instance can be initialized with a File object or a byte array. Follow the CodePath Camera and Gallery Guide guide for capturing photos with a camera.
// getPhotoFileUri() is defined in
// File photoFile = getPhotoFileUri(photoFileName);
ParseFile parseFile = new ParseFile(photoFile);
// val photoFile = getPhotoFileUri(photoFileName)
val parseFile = ParseFile(photoFile)
Rendering ParseFile objects
To render a ParseFile in an ImageView, you can use Parse’s UI Widget library and use the ParseImageView, which provides helper methods to load the image asynchronously. First, you need to add the Parse UI Widget to your app/ declaration:
Instead of an ImageView, use the class:
< android_layout_width="match_parent" android_layout_height="300dp"/>
To load the ParseFile, use the setParseFile() method and call loadInBackground():
tParseFile(tMedia());
imageView. loadInBackground()
tParseFile()
Alternatively, if you have a third-party library such as Glide or Picasso, you can also reference the URL directly and don’t need to use this special ParseImageView widget.
(ntext)(tMedia()())(imageView);
(ntext)()(imageView)
Geo Location
Parse has support for geolocation services and querying:
AnyWall Sample App – Sample app including how to read geolocation and use this data within your app.
Push Notifications
Parse supports push notifications made easy:
Parse Push Messaging – CodePath Guide
Parse Push – Super easy push notifications
Facebook SDK
For a quick way of incorporating Facebook login, check out Parse’s UI Library. It leverages Parse’s FacebookUtils library, which acts as a wrapper for associating ParseUser objects with Facebook users.
The official step by step instructions for integrating Parse with Facebook SDK is located here. The manual process of integrating with Facebook’s SDK is discussed below.
You will first need to create a Facebook app and get an Application ID.
If you are using open source Parse, make sure to set the FACEBOOK_APP_ID environment variable too.
You will also need to get access to your keystore hash and make sure to include it:
OS X:
keytool -exportcert -alias androiddebugkey -keystore ~/. android/ystore | openssl sha1 -binary | openssl base64
Windows:
keytool -exportcert -alias androiddebugkey -keystore%HOMEPATH%\. android\ystore | openssl sha1 -binary | openssl
base64
Next, you will need to include Parse’s FacebookUtils library, which provides an easy-to-use wrapper to work with the Facebook SDK, as well as Facebook’s SDK:
// add other parse dependencies
implementation ‘:facebook-android-sdk:4. 33. 0’
where X. X is the latest version:
You will then need to make sure to initialize these libraries by doing so in your file:
public class MainApplication extends Application {
itialize(new er(this). applicationId(“YOUR_APPLICATION_ID_HERE”). clientKey(null)
(“YOUR_HOSTED_PARSE_SERVER_URL/parse”)());
// ParseFacebookUtils should initialize the Facebook SDK for you
itialize(this);}
Note that you need to add /parse in the url in order for Parse to work, not just the hostname.
Make sure to reference this MainApplication in your file:
android_name=”. MainApplication”
Make sure to add the FacebookActivity to your manifest file, as well as the application ID and permissions you wish to request.


android_value=”email” />
There are two ways to support Facebook login: one is to use your own custom button/icon, or to use Facebook’s LoginButton class as described in the walkthrough. If you choose to use the LoginButton class, you should not use the FacebookUtils library to trigger a login. The reason is that Facebook’s LoginButton already has click handlers that will launch a login screen, so using Parse’s code triggers two of these screens to appear. In addition, you have to do more work to associate a ParseUser object with a Facebook user. For this reason, it is simpler to use the custom button approach when integrating with Parse.
Custom button
For your, add this code to trigger a login. If you wish to trigger this login, you may add your own custom button:

Leave a Reply

Your email address will not be published. Required fields are marked *