Android lint SharedPreferences.Editor.apply () warning

I upgraded to the latest Android SDK Tools (23.0.0), Platform-tools (20.0.0), the Android Studio Gradle plugin (0.12. +), And suddenly I get a weird Lint release report that says I should use apply () instead of commit (), since apply () is asynchronous and allows the user interface thread to continue, because commit () blocks it for writing. Cool. But still I get this:

enter image description here

Is this a Lint bug, or am I missing something here?

Obviously, I could suppress this warning, but I consider it meaningless and ignorant of the root cause.

EDIT: this will also be created when creating the application from the command line.

+6
source share
1 answer

This is a Lint bug. In particular, this one .

The error seems to be in the inner class CommitFinder SharedPrefsDetector :

 @Override public boolean visitMethodInvocation(MethodInvocation node) { ... String name = node.astName().astValue(); boolean isCommit = "commit".equals(name); if (isCommit || "apply".equals(name)) { ... if (returnValueIgnored) { String message = "Consider using apply() instead; commit writes " + "its data to persistent storage immediately, whereas " + "apply will handle it in the background"; mContext.report(ISSUE, node, mContext.getLocation(node), message, null); } 

I think the idea was to give this warning only if you did not assign the return value of commit() to anything (this part works), but they forgot to check the isCommit flag. :)

+5
source

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


All Articles