Writing to preferences with preferences.h, when do values get overwritten?

Using the ESP32. When I send a value to preferences.h to be written... does this value get written as well if the old value is the same as the new value? If not, should I make some logic to check this?

Let me explain with a code example:

#include <Preferences.h>

preferences.begin("storage", false);
preferences.putInt("drawer_1", 1);
preferences.end();

preferences.begin("storage", false);
preferences.putInt("drawer_1", 1);
preferences.end();

Does the above code writes two times or one time the Arduino memory.

Your question makes no sense to me. Please explain in more detail and provide context.

Thank you for the feedback, I have updated my question with a code example... hope all is clearer now.

Is this for an ESP32?

Hello,

I think it is not overwritten if the value is the same

marcwitteveen:
hope all is clearer now.

Could be even clearer. You are using an esp32? I can only find reference to that library for esp32, and your previous posts seem to be about esp32.

This is the Arduino forum, and esp32 is only one of many kinds of Arduino. You should always mention what hardware you are using when asking a question.

That library uses an ESP-IDF function nvs_set_i32. Looking at that page, I cannot see any mention of checking the if the value is different from the current value. So probably safest to assume it writes each time, even if the value is the same.

guix:
I think it is not overwritten if the value is the same

Good spot, guix.

Thanks guys, my bad yes it is for ESP32.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.