The commit and apply methods in SharedPreferences,

Source: Internet
Author: User

The commit and apply methods in SharedPreferences,

Preferences: ParametersSharedPreferences: Shared Parameters

1. Get the SharedPreferences object

In the context classThe getSharedPreferences method can obtain a SharedPreferences object.

private File makeFilename(File base, String name) { if (name.indexOf(File.separatorChar) < 0) { return new File(base, name); } throw new IllegalArgumentException( "File " + name + " contains a path separator"); }

The essence is to create a new File if the File does not exist.

2. commit Method

Source code comment:

 /** * Commit your preferences changes back from this Editor to the * {@link SharedPreferences} object it is editing. This atomically * performs the requested modifications, replacing whatever is currently * in the SharedPreferences. * * 

Note that when two editors are modifying preferences at the same * time, the last one to call commit wins. * *

If you don't care about the return value and you're * using this from your application's main thread, consider * using {@link #apply} instead. * * @return Returns true if the new values were successfully written * to persistent storage. */ boolean commit();

The commit method has a boolean return value. When data changes are stored, it is an atomic operation. When two editor objects operate on a shared preferences parameter at the same time, the last editor that calls the commit method always changes the final data value. 3. apply Method

Source code comment:

/*** Commit your preferences changes back from this Editor to the* {@link SharedPreferences} object it is editing. This atomically* performs the requested modifications, replacing whatever is currently* in the SharedPreferences.** 

Note that when two editors are modifying preferences at the same * time, the last one to call apply wins. * *

Unlike {@link #commit}, which writes its preferences out * to persistent storage synchronously, {@link #apply} * commits its changes to the in-memory * {@link SharedPreferences} immediately but starts an * asynchronous commit to disk and you won't be notified of * any failures. If another editor on this * {@link SharedPreferences} does a regular {@link #commit} * while a {@link #apply} is still outstanding, the * {@link #commit} will block until all async commits are * completed as well as the commit itself. * *

As {@link SharedPreferences} instances are singletons within * a process, it's safe to replace any instance of {@link #commit} with * {@link #apply} if you were already ignoring the return value. * *

You don't need to worry about Android component * lifecycles and their interaction with apply() * writing to disk. The framework makes sure in-flight disk * writes from apply() complete before switching * states. * *

The SharedPreferences.Editor interface * isn't expected to be implemented directly. However, if you * previously did implement it and are now getting errors * about missing apply(), you can simply call * {@link #commit} from apply(). */ void apply();

The apply method does not return a value. When two editors edit the preferences object at the same time, it is also the last object that calls the apply method to edit data. The submit operation of apply is also atomic.
4. Differences between the commit and apply methods:

Although commit and apply are both atomic operations, the atomic operations are different. commit is committed to the database by atoms. Therefore, the synchronization process from data submission to Disk exists and cannot be interrupted.

The atomic operation of the apply method is in the memory submitted by the atom instead of the database. Therefore, it cannot be interrupted when it is submitted to the memory, and then the data is submitted to the database asynchronously, therefore, there is no corresponding return value.

All commit commits are synchronous, and the efficiency is slower than that of the apply asynchronous commit. However, if the apply statement does not return a value, you will never know whether the storage fails.

The apply method is preferred when you do not care whether the submitted results are successful.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.