Reputation: 2246
I have 2 classes
- User
- StockProduct
StockProduct has a relation column sold_by for User.
I am trying to retrieve sold_by for the corresponding StockProduct but, it's returning 0 objects for the following code.
/**
* GET SELLER FOR CURRENT PRODUCT
* current_stock_object: ParseObject for StockProduct
*/
ParseRelation getSellerRelation = current_stock_object.getRelation("sold_by");
ParseQuery getSeller = getSellerRelation.getQuery();
getSeller.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> users, ParseException e) {
if (e!=null){
e.printStackTrace();
}
Log.d("SIZE", Integer.toString(users.size()));
}
});
I successfully retrieve the relation via Parse DataBrowser.
SIDE NOTE
/**
* GET PRICE FOR CURRENT PRODUCT
*/
ParseRelation<ParseObject> getPriceRelation = current_stock_object.getRelation("prices");
ParseQuery getPrice = getPriceRelation.getQuery();
getPrice.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject>prices, ParseException e) {
Log.d("PRICE SIZE", Integer.toString(prices.size()));
System.out.println(prices.get(0).get("costPrice"));
}
});
Works perfectly fine, leaving me with the thought that, getting ParseUser requires a different approach.
Any help would be appreciated.
Upvotes: 1
Views: 249
Reputation: 3118
I would try:
ParseRelation<ParseUser> getSellerRelation = current_stock_object.getRelation("sold_by");
getSellerRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> users, ParseException e) {
if (e!=null){
e.printStackTrace();
}
Log.d("SIZE", Integer.toString(users.size()));
}
});
All I added was an explicit type to the ParseRelation (in this case ParseUser). The documentation uses exactly this syntax, so I'm not sure why this wouldn't work for fetching the Relation.. maybe your "current_stock_object" needs to be fetched, or the "current_stock_object" is not the one you are looking at in the Parse console. Use the debugger and check the fields of your "current_stock_object" and ensure the objectId matches the one you are looking at in the console. Again, the object could be stale may need a fetch
Side note: (unrelated possibly)
Be sure to only use ParseRelation for a many-to-many relationship, otherwise just store the ParseObjects directly as a field. In this case, you have a StockProduct with a relation to _User table. If it makes sense in your application that a StockProduct can have multiple sellers, stick with the ParseRelation. If it was actually intended that a StockProduct may only have one unique seller, switch to not using a ParseRelation
Upvotes: 3