gpt4 book ai didi

java - SharedPreferences 在使用前不断被删除

转载 作者:行者123 更新时间:2023-12-02 09:03:08 30 4
gpt4 key购买 nike

这是一件好事。我将 int 值存储在我的共享首选项中,本质上是缓存它们,以便我可以在下次启动时将它们放入数据库中。我有一种方法可以在启动时将它们保存到数据库中。

接下来是从共享首选项中删除值以重置它们的方法。

但在我将它们存储到数据库之前,它们似乎一直在删除。就像无论我做什么,deleteMetrics(); 都会在 setMetrics(); 之前运行。

  • 我尝试将共享首选项值的删除放在自己的中启动 Activity 中的方法。
  • 我尝试过删除启动时 setMetrics 方法中的共享首选项值 Activity 。
  • 我尝试删除共享首选项值在启动 Activity 的 onCreate 的不同位置。
  • 我尝试将共享首选项值的删除放入startupUpdateMetrics() 内部和外部的 Metrics 类方法。
  • 我尝试将共享首选项值的删除放入runOnUiThread 以防我有泄漏线程(见下文)。
  • 我已在 onCreate 中尝试使用 Thread.sleep(5000) 来启动 settMetrics();deleteMetrics(); 之间的 Activity

没有任何效果。在 setMetrics(); 方法将值存储到数据库之前,这些值将被删除。

如果我注释掉deleteMetrics();,数据库值可以很好地存储,但它们会呈指数级增长,因为不会为每个应用程序使用重置sharedPrefs。

在创建时启动 Activity :

Activity_Splash.this.runOnUiThread(new Runnable() {
@Override
public void run() {
setMetrics();
deleteMetrics();
}
});

setMetrics();和deleteMetrics();

private void setMetrics() {
SharedPreferences sharedPreferences = Activity_Splash.this.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
//Check if device has a network connection
boolean connectionExists = new NetworkConnection().haveNetworkConnection(passedActivity, TAG);

//If device has a network connection continue
if (connectionExists) {
Log.i(" => ", "Test1");
try {
String userId = mAuth.getCurrentUser().getUid();
int teamCreated2 = sharedPreferences.getInt("teamCreatedForMetrics", 0);
Log.i(" => ", "Test2 " + teamCreated2);
callMetrics.startupUpdateMetrics(userId, context);
} catch (Exception e) {
Log.e(TAG + "onCreate", "Error setting metrics");
}
}
}

private void deleteMetrics() {
SharedPreferences sharedPreferences = Activity_Splash.this.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
try {
sharedPreferences.edit().remove("acceptedForMetrics").commit();
sharedPreferences.edit().remove("rejectedForMetrics").commit();
sharedPreferences.edit().remove("requestedForMetrics").commit();
sharedPreferences.edit().remove("sm2AppointedForMetrics").commit();
sharedPreferences.edit().remove("teamCreatedForMetrics").commit();
sharedPreferences.edit().remove("teamDeletedForMetrics").commit();
sharedPreferences.edit().remove("noOfPlayersForMetrics").commit();
sharedPreferences.edit().remove("teamsMemberLeaveForMetrics").commit();
} catch (Exception e) {
Log.e(TAG + "onCreate", "Error setting metrics");
}
}

callMetrics 被 setMetrics 方法调用:

public void startupUpdateMetrics(String userId, Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);

db.collection("userteammetrics").document(userId).collection("teammetrics").document("generalteammetrics").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
int teamCreated4 = sharedPreferences.getInt("teamCreatedForMetrics",0);
Log.i(" => ", "Test4 " + teamCreated4);
DocumentSnapshot document = task.getResult();

if (document.exists()) {
int teamCreated5 = sharedPreferences.getInt("teamCreatedForMetrics",0);
Log.i(" => ", "Test5 " + teamCreated5);
int oldAccepted = Integer.parseInt(document.get("accepted").toString());
int oldRejected = Integer.parseInt(document.get("rejected").toString());
int oldRequested = Integer.parseInt(document.get("requested").toString());
int oldsm2Appointed = Integer.parseInt(document.get("scrummaster2sappointed").toString());
int oldTeamsCreated = Integer.parseInt(document.get("teamscreated").toString());
int oldTeamsDeleted = Integer.parseInt(document.get("teamsdeleted").toString());
int oldTeamsMemberLeave = Integer.parseInt(document.get("memberLeave").toString());

int addAccepted = sharedPreferences.getInt("acceptedForMetrics",0);
int addRejected = sharedPreferences.getInt("rejectedForMetrics",0);
int addRequested = sharedPreferences.getInt("requestedForMetrics",0);
int addsm2Appointed = sharedPreferences.getInt("sm2AppointedForMetrics",0);
int addTeamsCreated = sharedPreferences.getInt("teamCreatedForMetrics",0);
int addTeamsDeleted = sharedPreferences.getInt("teamDeletedForMetrics",0);
int playersPerGame = sharedPreferences.getInt("noOfPlayersForMetrics",0);
int addTeamsMemberLeave = sharedPreferences.getInt("teamsMemberLeaveForMetrics",0);
Log.i("Test6 ", "startupUpdateMetrics: " + addAccepted + " " + addRejected + " " + addRequested + " " + addsm2Appointed + " " + addTeamsCreated + " " + addTeamsDeleted + " " + playersPerGame + " " + addTeamsMemberLeave);

int newAccepted = oldAccepted + addAccepted;
int newRejected = oldRejected + addRejected;
int newRequested = oldRequested + addRequested;
int newsm2Appointed = oldsm2Appointed + addsm2Appointed;
int newTeamsCreated = oldTeamsCreated + addTeamsCreated;
int newTeamsDeleted = oldTeamsDeleted + addTeamsDeleted;
int newTeamsMemberLeave = oldTeamsMemberLeave + addTeamsMemberLeave;
Log.i("Test7 ", "startupUpdateMetrics: " + newAccepted + " " + newRejected + " " + newRequested + " " + newsm2Appointed + " " + newTeamsCreated + " " + newTeamsDeleted + " " + newTeamsMemberLeave);

db.collection("userteammetrics").document(userId).collection("teammetrics").document("generalteammetrics").update("accepted", newAccepted, "rejected", newRejected, "requested", newRequested, "scrummaster2sappointed", newsm2Appointed, "teamscreated", newTeamsCreated, "teamsdeleted", newTeamsDeleted, "memberLeave", newTeamsMemberLeave, "numberofplayerspergame" ,FieldValue.arrayUnion(playersPerGame));

}
}
}
});
}

LogCat(我不知道为什么它全部出现两次......因此我认为可能存在泄漏线程):

2020-02-01 23:19:28.954 21586-21586/? I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("xxxxxxxxxxxxx") to get test ads on this device.
2020-02-01 23:19:29.026 21586-21586/? I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("xxxxxxxxxxxxx") to get test ads on this device.
2020-02-01 23:19:29.078 21586-21586/? I/ =>: Test1
2020-02-01 23:19:29.078 21586-21586/? I/ =>: Test2 0
2020-02-01 23:19:29.085 21586-21586/? I/ =>: Test1
2020-02-01 23:19:29.085 21586-21586/? I/ =>: Test2 0
2020-02-01 23:19:29.207 21586-21586/? I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("xxxxxxxxxxxxx") to get test ads on this device.
2020-02-01 23:19:29.219 21586-21586/? I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("xxxxxxxxxxxxx") to get test ads on this device.
2020-02-01 23:19:29.344 21586-21586/? I/ =>: Test4 0
2020-02-01 23:19:29.344 21586-21586/? I/ =>: Test5 0
2020-02-01 23:19:29.345 21586-21586/? I/Test6: startupUpdateMetrics: 0 0 0 0 0 0 0 0
2020-02-01 23:19:29.345 21586-21586/? I/Test7: startupUpdateMetrics: 0 0 0 0 0 0 0
2020-02-01 23:19:29.345 21586-21586/? I/ =>: Test4 0
2020-02-01 23:19:29.346 21586-21586/? I/ =>: Test5 0
2020-02-01 23:19:29.346 21586-21586/? I/Test6: startupUpdateMetrics: 0 0 0 0 0 0 0 0
2020-02-01 23:19:29.346 21586-21586/? I/Test7: startupUpdateMetrics: 0 0 0 0 0 0 0
2020-02-01 23:21:04.323 21908-21927/? I/PrimesTesting: GserviceFlagsSupplier.get()
2020-02-01 23:22:40.049 22041-22041/? D/OmtpVvmCarrierCfgHlpr: overrideConfigForTest is null

我对此感到茫然,任何建议将不胜感激。

最佳答案

您可以尝试设置一个计时器来连续检查 setMetrics() 是否已完成执行其所有代码,以便之后启动 deleteMetrics() 方法。

private static boolean done = false;

Activity_Splash.this.runOnUiThread(new Runnable() {
@Override
public void run() {
setMetrics();
final Timer t=new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
if(done){
deleteMetrics();
t.cancel();
t.purge();
}
}
}, 1000);

}
});

setMetrics() 末尾将 done 的 boolean 值分配给 true

 private void setMetrics() {
SharedPreferences sharedPreferences = Activity_Splash.this.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
//Check if device has a network connection
boolean connectionExists = new NetworkConnection().haveNetworkConnection(passedActivity, TAG);

//If device has a network connection continue
if (connectionExists) {
Log.i(" => ", "Test1");
try {
String userId = mAuth.getCurrentUser().getUid();
int teamCreated2 = sharedPreferences.getInt("teamCreatedForMetrics", 0);
Log.i(" => ", "Test2 " + teamCreated2);
callMetrics.startupUpdateMetrics(userId, context);
done=true;
} catch (Exception e) {
Log.e(TAG + "onCreate", "Error setting metrics");
}
}
}

关于java - SharedPreferences 在使用前不断被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022022/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com