SharedPreferences会在使用前不断删除

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SharedPreferences会在使用前不断删除相关的知识,希望对你有一定的参考价值。

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

这后面是从共享的首选项中删除值以重置它们的方法。

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

  • 我尝试将删除共享的pref值放在自己的位置启动活动中的方法。
  • 我已尝试将启动时setMetrics方法中的共享首选项值活动。
  • 我已尝试删除共享的pref值在启动活动的onCreate中的不同位置。
  • 我尝试将删除的共享pref值放入Metrics内部和外部的startupUpdateMetrics()类方法。
  • 我曾尝试将共享的pref值删除如果线程泄漏(请参见下文),请输入runOnUiThread
  • 我已经在onCreate中尝试在settMetrics();deleteMetrics();之间进行启动活动的Thread.sleep(5000)>
  • 没有任何作用。在setMetrics();方法将值存储到DB之前,这些值将被删除。

如果我注释掉deleteMetrics();,DB值会很好地存储,但是它们会成倍增加,因为没有为每种应用使用而重置sharedPrefs。

在onCreate上启动活动:

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");
        
    

setMetrics方法调用的callMetrics:

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

我对此感到困惑,任何建议将不胜感激。

这是斗士。我将int值存储在共享的pref中,本质上将它们缓存,以便下次启动时可以将它们放入数据库中。我有一种在启动时将它们保存到数据库的方法。随后是...

答案

发生的事情:

以上是关于SharedPreferences会在使用前不断删除的主要内容,如果未能解决你的问题,请参考以下文章

SharedPreferences工具类

SharedPreferences工具类

SharedPreferences三种获取方式的不同

带有 SharedPreferences 的 Android 服务 setInexactRepeating asyncTask 问题

boolean变量不断变为false

为啥查看WINDOWS文件夹的属性里大小在不断变化呢?