Sunday 23 November 2014

removing and add one value in array saved in Parse table with Parse.com

Hello all,

In the Previous Post ,I have explained how to add the array and Parse File in Parse table

Now let's move to another step in Parse.com series which is to remove one value from the array and also adding one value in an array which is saved in Parse table 

The difference : in the Previous Post I have explained to add the whole array in Parse table , But In this post I am going to explain how to modify (add/remove) the array which is saved in Parse DB

Let's make a Parse table named "channel" having  usersArray (datatype : array) column 

Now first we will add a value in the usersArray in "channel" . For this we have to follow following steps: 
1: Assume an List<String> list1= new ArrayList<String>
2: Check usersArray  is null or not
3: Assign the value of usersArray to list1
4: Check that list1 contains that value (which is need to be added)
5: If not Add the value in list1 .
6: save the list1 in usersArray in channel table.

For this please check the below code to add one value in array :

                 List<String> list11 =  new ArrayList<String>();
String object_id = "123456"; //change this according to your need.
ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("Channel");
pQuery.whereEqualTo("objectId",object_id);
pQuery.findInBackground(new FindCallback<ParseObject>() {

@Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("usersArray")!=null) {
list11 =  p.getList("usersArray");
}
else
{
list11= null;
}

if (list11!=null) {
if (!list11.contains(user_id)) {
list11.add(user_id);
}
                                                }
if (list11!=null) 
p.put("usersArray", list11);
else
p.put("usersArray",null );
   p.saveInBackground(new SaveCallback() {

@Override
public void done(ParseException arg0) {
                     Toast.makeText(mActivity,"done",Toast.LENGTH_LONG).show();
}
});
}
}
}
}

});


Now let's move to remove one value from the usersArray in "channel" . For this we have to follow following steps: 
1: Assume an List<String> list1= new ArrayList<String>
2: Check usersArray  is null or not
3: Assign the value of usersArray to list1
4: Check that list1 contains that value (which is need to be added)
5: If yes remove the value from list1 .
6: save the list1 in usersArray in channel table again.

For this please check the below code to remove one value from saved array :

  final List<String> list11 =  new ArrayList<String>();
String object_id = "123456"; //change this according to your need.
ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("Channel");
pQuery.whereEqualTo("objectId",object_id);
pQuery.findInBackground(new FindCallback<ParseObject>() {

@Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("usersArray")!=null) {
list11 =  p.getList("usersArray");
}
else
{
list11= null;
}

if (list11!=null) {
if (list11.contains(user_id)) {
list11.remove(user_id);
}
                                       }
if (list11!=null) 
p.put("usersArray", list11);
else
p.put("usersArray",null );
p.saveInBackground(new SaveCallback() {

@Override
public void done(ParseException arg0) {
              Toast.makeText(mActivity,"done",Toast.LENGTH_LONG).show();
}
});
}
}}
});

That's all..for remove and add one value from/to the array which is saved in Parse table with Parse.com.
In above code I have applied condition on the basis of objectId  you have to change according to your need.
Please leave any comment if you have any query.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...