DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Google Cloud Messaging with Android
  • Top 13 Mobile App Development Platforms You Need
  • Exploring the Salesforce Mobile SDK Using Android Studio
  • How Is AI Used in Android App Development?

Trending

  • A Better Web3 Experience: Account Abstraction From Flow (Part 2)
  • The Systemic Process of Debugging
  • API Design
  • Mastering Persistence: Why the Persistence Layer Is Crucial for Modern Java Applications
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Parse.com Anonymous and Registered Users (Android)

Parse.com Anonymous and Registered Users (Android)

Jamie Craane user avatar by
Jamie Craane
·
Jan. 02, 15 · Interview
Like (0)
Save
Tweet
Share
11.64K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction
When writing a mobile application you mostly always need a way to store the information outside of the application itself so the data is accessible to not only the application itself on a specific device, but on every device the application is installed on and perhaps even web applications. This means the application will need some sort of a backend service/API to communicate with. This usually also means the application will use some sort of account/user management.

There are several so-called mobile backend as a service platform which facilitate this. The word mobile is a bit misplaced I think because the clients of those platforms are not always mobile devices.  I therefor call those platforms Backend As a Service (BAAS). Parse.com is one of those platforms.

This post talks about the way you can manage users with Parse.com and specifically how to use anonymous users and convert between an anonymous and registered user.

Anonymous user
A lot of apps require the user to register (create a user account) or login with a Facebook or similar account. If this is a required process, chances are that a certain group of users will not use your app because of the required login. If your app functionality allows, a possible work-around for this is to provide the app with anonymous user login. By using an anonymous user, the user of your app can experience all or most of the functionality of the app without requiring a user account. If the user likes your app, he/she can then sign-up for a registered account. Ideally, all of the data gathered during anonymous access, should be transferred to the registered account. Fortunately, the above functionality is fairly easy to implement using the Parse.com platform.

Enabling anonymous access
To enable your Parse.com app for using anonymous access, you have to do the following:

1. Enable anonymous access in the Parse.com console. Go to Settings -> Authentication and enable "Allow anonymous access".

2. Add the following code in the onCreate method of your Android Application class:

@Override
    public void onCreate() {
        super.onCreate();
        Parse.initialize(this, "APPLICATION_ID", "CLIENT_KEY");
        ParseUser.enableAutomaticUser();
}


By enabling automatic user, the call to ParseUser.getCurrentUser() always returns a user and thus is never null. You can check if the user is an anonymous user or a registered one by using the following code:

ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser());

This can be useful to check if the sign-up button should be displayed or disabling some functionality which is only accessible to registered users.

Converting an anonymous user into a registered one
An anonymous user can be converted to a registered one. The data belonging to the anonymous user is also present on the registered one.

Before converting an anonymous user, there are some things to consider:

  1. The username can not be left blank. You must explicitly specify a username and password on the user which is to be converted into a registered one.
  2. It is adviced to save the anonymous user to the backend as soon as it is created. If this is not done and a call to saveInBackground is called on the registered user (after converting it from an anonymous one) a stack overflow is generated from the Android parse SDK. See also the following question on Stack-overflow (created by me): http://stackoverflow.com/questions/27595057/converting-an-anonymous-user-to-a-regular-user-and-saving
To save the user immediately after it is created, modify the Application code so that it looks like this:
@Override
    public void onCreate() {
        super.onCreate();
        Parse.initialize(this, "APPLICATION_ID", "CLIENT_KEY");
        ParseUser.enableAutomaticUser();
        ParseUser.getCurrentUser.saveInBackground();
}

The anonymous user can now be converted into a registered one with the following code:
findViewById(R.id.createUser).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final String accountUsername = username.getText().toString();
                final String accountPassword = password.getText().toString();
                final ParseUser user = ParseUser.getCurrentUser();
                user.setUsername(accountUsername);
                user.setPassword(accountPassword);
                user.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(final ParseException e) {
                        if (e != null) {
                            Toast.makeText(MainActivity.this, "Signup Fail", Toast.LENGTH_SHORT).show();
                            Log.e(TAG, "Signup fail", e);
                        } else {
                            Toast.makeText(MainActivity.this, "Signup success", Toast.LENGTH_SHORT).show();
                            final ParseUser user = ParseUser.getCurrentUser();
                            user.put("phone_no", "31612345678");
                            user.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(final ParseException e) {
                                    if (e != null) {
                                        Toast.makeText(MainActivity.this, "Save data Fail", Toast.LENGTH_SHORT).show();
                                        Log.e(TAG, "Signup fail", e);
                                    } else {
                                        Toast.makeText(MainActivity.this, "Save data success", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }
                });
            }
        })

Please note that the data associated with the user in the saveInBackground call (after sign-up is successful) could also be associated immediately to the user before the signUp call. This saves an extra network call. The call to saveInBackground is pure for demonstration purposes.

Conclusion
This post showed the benefits of an anonymous user of a mobile app and how the anonymous user can be used with the Parse.com platform. It also showed code examples of how an anonymous user is converted into a registered one and the potential problems and solutions with it.
mobile app Android (robot)

Published at DZone with permission of Jamie Craane, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Google Cloud Messaging with Android
  • Top 13 Mobile App Development Platforms You Need
  • Exploring the Salesforce Mobile SDK Using Android Studio
  • How Is AI Used in Android App Development?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: