Pablo Garcia Pablo Garcia - 6 months ago 34
Android Question

Android Parse Pointer return empty data

I have the next table in Back4App(Parse)

Match: id(String), maxPlayers(Int), sport(ParseObject)

The problem is when I try to get all the match objects sometimes (I don't know why sometimes run, and others doesn't run) I get the sport parse object with no data. Other times as I said, I get the object right. Here is the code:

public String saveMatch(Match match) throws ParseException {

ParseObject parseObject = new ParseObject("match");

parseObject.put(COLUMN_MAX_PLAYERS, match.getMaxPlayer());

//ParseObject sport = new ParseObject("Sport");
//sport.setObjectId(match.getSport().getId());
//parseObject.put("sport", sport);

//I change to the next code but the problem continue.
ParseObject sport = ParseObject.createWithoutData(TableSport.TABLE_NAME,match.getSport().getId());
parseObject.put(COLUMN_SPORT, sport);

parseObject.save();

return parseObject.getObjectId();

}

@Override
public ArrayList<Match> getAllMatches() throws ParseException, ParseObjectException {

ParseQuery<ParseObject> query = ParseQuery.getQuery(TABLE_NAME);
List<ParseObject> objects = query.find();

return parseAllObjectsToMatchList(objects);
}

private ArrayList<Match> parseAllObjectsToMatchList(List<ParseObject> objects) throws ParseException, ParseObjectException {

ArrayList<Match> matches = new ArrayList<>();
for (int i = 0; i < objects.size(); i++){
matches.add(parseObjectToMatch(objects.get(i)));
}
return matches;
}

private Match parseObjectToMatch(ParseObject object) throws ParseException, ParseObjectException {

try{
Match match = new Match();

match.setId(object.getObjectId());
match.setMaxPlayer(object.getInt(COLUMN_MAX_PLAYERS));

ParseObject objectSport = object.getParseObject(COLUMN_SPORT);
if(objectSport!= null){
Sport sport = (sportDatabaseManager.parseObjectToSport(objectSport));
match.setSport(sport);
}

return match;

}catch (Exception e){
throw new ParseObjectException();
}
}

private Sport parseObjectToSport(ParseObject parseObject) throws ParseObjectException {

try{
ParseFile parseFile = parseObject.getParseFile(TableSport.COLUMN_IMAGE);

Sport sport = new Sport();
sport.setId(parseObject.getObjectId());
sport.setName(parseObject.getString(TableSport.COLUMN_NAME));
sport.setSumary(parseObject.getString(TableSport.COLUMN_SUMARY));
sport.setDescription(parseObject.getString(TableSport.COLUMN_DESCRIPTION));
sport.setImage(parseFile.getUrl());

return sport;
}catch (Exception e){
throw new ParseObjectException();
}
}

Answer

use 'include' to get the related data

@Override
public ArrayList<Match> getAllMatches() throws ParseException, ParseObjectException {

    ParseQuery<ParseObject> query = ParseQuery.getQuery(TABLE_NAME);
    query.include(COLUMN_SPORT);
    List<ParseObject> objects = query.find();

    return parseAllObjectsToMatchList(objects);
}

https://parse.com/docs/android/guide#queries-relational-queries https://parse.com/docs/android/api/com/parse/ParseQuery.html#include(java.lang.String)