1

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();
    }
}

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution if anyone need it.

The problem is the pointer objects take a while to get retrieved so I need to call parseObject.fetchIfNeeded().

1 Comment

fetchIfNeeded() returns the object but it is empty. There is no estimated data in it. Is there any solution for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.