When will the SharedPreferences () and commit () actions be applied?

I use SharedPreferences to save user data in my application. I know the difference between the methods commit()and apply(), but I noticed that it commit()returns true if the new values ​​were successfully written to the persistent storage, but apply()not.

What are the reasons the method commit()returns false or apply()for failure?

+4
source share
1 answer

Let's see the source code for commit () :

    public boolean commit() {
        MemoryCommitResult mcr = commitToMemory();
        SharedPreferencesImpl.this.enqueueDiskWrite(
            mcr, null /* sync write on this thread okay */);
        try {
            mcr.writtenToDiskLatch.await();
        } catch (InterruptedException e) {
            return false;
        }
        notifyListeners(mcr);
        return mcr.writeToDiskResult;
    }

, commit() false, - ( , - , ).

+4

Source: https://habr.com/ru/post/1613702/


All Articles