1

我在 Back4App(Parse) 中有下一张表

匹配:id(String)、maxPlayers(Int)、sport(ParseObject)

问题是当我有时尝试获取所有匹配对象时(我不知道为什么有时会运行,而其他人不运行)我得到了没有数据的运动解析对象。正如我所说,其他时候,我得到了正确的对象。这是代码:

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

2 回答 2

1

使用'include'获取相关数据

@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)

于 2016-05-31T13:45:53.417 回答
0

如果有人需要,我找到了解决方案。

问题是指针对象需要一段时间才能被检索到,所以我需要调用 parseObject.fetchIfNeeded()。

于 2016-05-30T11:53:58.687 回答