Sunday 16 November 2014

Add different types of data in Parse Table and fetch them from the Parse table in Parse.com

Hello all,

In the previous post I have explained how to signup and login as a Parse User in Parse Table with the help of Parse.com sdk

Now I am going to add one another tutorial in Parse.com series.

Today I am going to share the code to add different types of data(string,int,boolean) in the Parse Table and then explains the code to fetch them.

First you have to create a table in Parse database manually,then add the columns with different data types according to your need

I have made a Parse Table named "channel" and add the following columns  in it :

1. name(String)
2. isPrivate(boolean)
3. maxSize(int)

Now below is the code to add the values to the Parse table(here  "channel") in runtime:

boolean b_privacy = false;
String s_name = "XYZ";
int max_size = 50;

 final ParseObject testObject = ParseObject.create("channel");

                 testObject.put("isPrivate",b_privacy);
testObject.put("maxSize",max_size);
 testObject.put("name",s_name);
 testObject.saveInBackground(new SaveCallback() {

@Override
public void done(ParseException e) {

 if (e == null) {
Toast.makeText(getApplicationContext(),"Channel Added            Succesfully",Toast.LENGTH_LONG).show();
 } 
                                        else {
Toast.makeText(getApplicationContext(),"Something went wrong.Please try again later",Toast.LENGTH_LONG).show();

   }
}
});

After that lets move to the second part of the post that is to fetch the data from the Parse table in runtime:

Let's us assume to fetch the data from the "channel" table.Now we have to fetch the name(String) , isPrivate (boolean) and  maxsize(int).

Here is the code to fetch the data from "channel" table:

Note: Here I am fetching the data on the basis of objectId.

            ParseQuery<ParseObject> query = ParseQuery.getQuery("channel");
   query.whereEqualTo("objectId",objid);
   query.findInBackground(new FindCallback<ParseObject>() {
       public void done(List<ParseObject> userList, ParseException e) {
       
                if (e == null) {
          
            Log.d(">>",">>"+userList.size());
            if (userList.size()>0) {

        ParseObject p = userList.get(0);

String s_name = p.getString("name");
boolean is_private = p.getBoolean("isPrivate");
int max_size =p.getInt("maxSize");

        }} 
              else {
               // error
            Log.d(">>",">>"+e);
           }
       }
   });


Note : if you want to fetch or add the data in Parse User table you have to replace this line :
ParseQuery<ParseObject> query = ParseQuery.getQuery("channel");
by
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");

->"_User" is used to represent the ParseUser table .

That's all .Hope it will be useful for you . thanks


No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...