【发布时间】:2014-06-18 19:52:53
【问题描述】:
我正在开发使用 Parse.com 作为后端的 Android 应用程序。
Parse 的 Android SDK 的文档在section related to the Local datastore feature 中包含以下代码:
ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.orderByDescending("score");
// Query for new results from the network.
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scores, ParseException e) {
// Remove the previously cached results.
ParseObject.unpinAllInBackground("highScores", new DeleteCallback() {
public void done(ParseException e) {
// Cache the new results.
ParseObject.pinAllInBackground("highScores", scores);
}
});
}
});
但是,如果我尝试在我的代码中使用它,我会收到错误提示
不能在内部类中引用非最终变量 score 用不同的方法定义
上线ParseObject.pinAllInBackground("highScores", scores);
我明白问题可能是什么,问题是如何解决它。
我的想法
- 通过在 done 方法中创建局部最终变量,例如
final List<ParseObject> localScores = scores然后在pinAllInBackground中使用它 - 通过将
done方法的签名更改为public void done(finalList<ParseObject> scores, ParseException e)
这两个选项似乎都有效。
我想从经验丰富的 Java 开发人员那里得到答案,如果一种选择比另一种更好,为什么?
我个人对选项 2. 起作用感到惊讶,但似乎 final 修饰符不会导致方法重载 - 这里很好地证明了我对 Java 缺乏更深入的了解 :-)
附:让我们抛开官方文档包含非工作代码 sn-ps 的事实。
【问题讨论】:
标签: java android parse-platform