![](/img/trans.png)
[英]How can I stop my android app from force closing when it cant connect to my server?
[英]Parse Local Datastore: I can't access ACL secured objects from the network after force closing the app? (Android)
成功登錄后,我會打電話給:
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.isAuthenticated()
現在,如果我通過主頁按鈕或多任務切換到另一個應用程序並返回到我的應用程序,則仍然對currentUser進行身份驗證。
但是,如果我強制關閉該應用程序,然后重新打開它,則當前用戶未通過身份驗證。 因此,看來我無法通過以下方式訪問網絡中添加了默認訪問控制列表(ACL)的任何對象:
Parse.enableLocalDatastore(this);
ParseACL.setDefaultACL(new ParseACL(), true);
// Pinning
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.pinInBackground("NEW_GAMESCORES", null);
// Syncing Local Changes
ParseQuery<ParseObject> localQueryNewScores = ParseQuery
.getQuery("GameScore");
localQueryNewScores.fromPin("NEW_GAMESCORES");
localQueryNewScores.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scores, ParseException e) {
Log.d("score", "New scores = " + scores.size());
for (ParseObject score : scores) {
score.saveInBackground();
score.unpinInBackground("NEW_GAMESCORES", null);
score.pinInBackground("GAMESCORES", null);
}
}
});
// Syncing Network Changes
ParseQuery<ParseObject> networkQueryScores = ParseQuery
.getQuery("GameScore");
// Query for new results from the network.
networkQueryScores.findInBackground(new FindCallback<ParseObject>() {
public void done(final List<ParseObject> scores, ParseException e) {
Log.d("score", "Network scores = " + scores.size());
// Remove the previously cached results.
ParseObject.unpinAllInBackground("GAMESCORES",
new DeleteCallback() {
public void done(ParseException e) {
// Cache the new results.
ParseObject.pinAllInBackground("GAMESCORES",
scores);
}
});
}
});
// Querying the Local Datastore
ParseQuery<ParseObject> localQueryScores = ParseQuery
.getQuery("GameScore");
localQueryScores.fromPin("GAMESCORES");
localQueryScores.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scores, ParseException e) {
Log.d("score", "Local scores = " + scores.size());
}
});
我多次運行代碼后才記錄日志輸出:
New scores = 2
Local scores = 0
Network scores = 0
New scores = 0
Local scores = 0
Network scores = 2
New scores = 2
Local scores = 2
Network scores = 2
New scores = 1
Local scores = 2
Network scores = 4
我強制關閉應用程序后的日志輸出:
New scores = 0
Local scores = 4
Network scores = 0
New scores = 2
Local scores = 0
Network scores = 0
如您所見,在強制關閉后Network scores = 0
,我無法從網絡中查詢任何結果,我// Query for new results from the network
從網絡中// Query for new results from the network
更新本地數據存儲區中的固定對象。 即使我在首次登錄后仍不斷連接到互聯網,也會發生這種情況。
但是由於我需要將更改從網絡同步回本地數據存儲,所以我要依賴此查詢。
因此,在我強制關閉應用程序之后,如何仍可以在網絡中查詢用ACL存儲到currentUser中的對象?
我發現其他人也遇到了同樣的問題,這里已經報道過: developers.facebook.com/bugs/702967266408226
在新的Parse Android SDK 1.5中似乎是一個錯誤。 很明顯我的問題與所報告的錯誤有關,我將立即更新此帖子。
也許將密碼保存到文件,如果在強制關閉后重新打開應用程序,則使用此保存的密碼登錄?
您可以將用戶名和密碼保存在SharedPreferences中,然后在用戶重新打開您的應用程序時再次登錄。
我認為您可以只檢查getCurrentUser
是否為null而不是使用isAuthenticated
。
boolean isLoggedIn = ParseUser.getCurrentUser() != null;
ParseUser是否已在此設備上進行了身份驗證。 如果ParseUser是通過logIn或signUp方法獲得的,則為true。 只有經過身份驗證的ParseUser可以保存(屬性已更改)並刪除。
getCurrentUser
始終是已注冊或登錄的用戶。因此,如果它不為null,則表示該用戶已登錄isAuthenticated
只是為了查看是否從登錄/注冊中檢索到該用戶對象。 例如,如果您查詢其他ParseUser
對象,則將不對它們進行身份驗證。
還要注意,當您對當前用戶進行logOut
時, getCurrentUser
將為null。
我還注意到,如果您不使用本地數據存儲,則即使強制關閉應用程序, currentUser.isAuthenticated()
也會始終返回true。
似乎當您使用本地數據存儲時,在本地檢索當前用戶,該對象不再被視為“已認證”,因為源中不是來自登錄或注冊的對象。
解析Android Changelog
v1.5.1-2014年5月30日
修復了本地數據存儲區的各種錯誤。
問題確實是本地數據存儲區錯誤,該錯誤已在今天通過新的Parse Android SDK版本1.5.1修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.