我在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();
}
}
答案 0 :(得分: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)
答案 1 :(得分:0)
如果有人需要,我找到了解决方案。
问题是指针对象需要一段时间才能被检索,所以我需要调用parseObject.fetchIfNeeded()。