Hi
I’m using custom class Seller which contains list type of related object: List<BackendlessUser>Followers;
It is okay to add new user item to Seller instance to Followers list and save it at cloud. But when i tried to delete user item at list, it was not work actually. I deleted item from Seller instance’s Follower(list), set a new Followers to Seller, and saved it. Bellows are my code that updating Seller’s Followers
public class AccentAction extends Thread {
private static final String TAG = "AccentActionThread";
String sid;
Context context;
int mark;
public AccentAction(String s, Context c, int m){
sid = s;
context = c;
mark = m; // 0: unfollowing - delete, 1: following - add
}
@Override
public void run() {
Seller sel = Backendless.Data.of(Seller.class).findById(sid);
if(sel == null) return;
List<BackendlessUser> followers = sel.getFollowers();
BackendlessUser me = Backendless.UserService.CurrentUser();
boolean changed = false;
if (followers == null) {
if (mark == 0) {
Log.v(TAG, "no need to delete element from list");
return;
}
followers = new ArrayList<BackendlessUser>();
followers.add(me);
Log.v(TAG, "set new list to seller with new list");
sel.setFollowers(followers);
changed = true;
} else {
if(mark == 1) {
boolean found = false;
for(BackendlessUser u : followers) {
if(u.getObjectId().equals(me.getObjectId())) {
found = true;
break;
}
}
if(!found) {
sel.getFollowers().add(me);
Log.v(TAG, "need to adding user to list");
changed = true;
}
}else{
for(int i =0; i<sel.getFollowers().size(); i++) {
if(sel.getFollowers().get(i).getObjectId().equals(me.getObjectId())) {
Log.v(TAG, "item found");
BackendlessUser u = sel.getFollowers().remove(i);
if(u==null) {
Log.e(TAG, "fail at deleting..");
}else{
changed = true;
Log.v(TAG, "deleting completed" + u.getObjectId());
}
break;
}
}
}
}//
if(changed) {
Seller seller = Backendless.Data.of(Seller.class).save(sel);
if(seller == null) {
Log.e(TAG, "Error at updating");
}else{
Log.v(TAG, "updating ends");
if(seller.getFollowers() != null) {
Log.v(TAG, "after list size" + String.valueOf(seller.getFollowers().size()));
}
}
}else{
Log.v(TAG, "no need to update");
}
}
}
At the line 66, i checked that after saved Seller instance has a reduce size of Followers(List<BackendlessUser). But real problem is that when i reload the seller object from cloud after enough time past from updating, the Seller’s Follow(list) size was same and the user was not deleted.
I tried to this job by using both Sync and Async call at Thread and Main Thread, but result is same… What am i doing wrong? Please help me.
I tested relation data add and delete at list with another custom classes, All was fine. But only this case that related list has BackenlessUser objects was not working. Is it a authority problem of user table?
Sure, the table has only one object now. At console i manually added 1 test ‘Seller’ row. Then i programmatically added 3 users to it’s ‘Followers’ list and saved from my mobile side. After about 5 minutes, i tried to delete 2 users from test ‘Seller’ object’s ‘Followers’ list. However by reloading test object’s list by find() operation from client side or checking it’s relation at the console, the result was same.
Can you show me your code what you’ve tested? I don’t know which part of my code is wrong. All i used related data works fine except this BackendlessUser related one.
package com.mbaas;
public class Bar
{
public String name;
}
Class Foo:
package com.mbaas;
import java.util.ArrayList;
public class Foo
{
public String name;
public ArrayList<Bar> bars;
}
Test code:
using findLast since I had multiple foo objects in the table and the last one was created using backendless console. The object had a collection of two “bar” objects. After I ran the code, one of the bar objects was deleted (verified in console). The code uses sync API so I had to run it in a new thread to avoid problems with the main UI thread:
Hi Mark, I’m having the same issue here where I’m removing items (Recipe objects) from a List in my main object (Party object) but it’s not updating on the server. The interesting thing is, I’m seeing the correct amount of (Recipe) items in my List when my main Object (Party) returns, but again, no change on the server. My code looks similar to yours:
public class Party extends ModelObject {
...
private List<Recipe> menuItems = new ArrayList<>();
...
}
public class Recipe extends ModelObject {
...
private List<Ingredient> ingredients;
private String language;
...
}
Backendless.Persistence.of(Party.class).save(mParty, new AsyncCallback<Party>() {
@Override
public void handleResponse(Party party) {
PartyPlannerLog.v(WHOAMI, "Party Saved");
}
@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(partyViewActivity, "Error saving party!", Toast.LENGTH_SHORT).show();
}
});
So in the handleResponse code block, the menuItems List contains the correct items, however, i’m not seeing the change on the server. Any ideas?
Btw, I can add Recipe items to the list without a problem, but removing them is when I don’t see the change on the server.
Just double checked the functionality in the topic.
I have an Order table and ShoppingItem table, with one order to many items relation
In the test I retrieve first order from the database, print the information about order items, delete first item from the list, save order and print the information about order items again. Both versions (client and server).
I tried adding the “__meta” property to my custom Class (I’m doing this in Flex/AIR), and that did the trick. I guess the server needs some data from there in order to go through with the update.