Page 1 of 1

NVS set the same value as stored in nvs partition

Posted: Thu Sep 10, 2020 1:51 pm
by Sweaps
Hello,

I'm working on ESP-IDF v4.0.1

I would like to know what happens if I call nvs_set_* and I already have got the same value stored in my nvs partition ?
In my code, I don't know if I need to check the previous value stored before storing a new one and I don't find a good information into the docs.

I found the followings information in https://docs.espressif.com/projects/esp ... flash.html:
Keys are required to be unique. Assigning a new value to an existing key works as follows:

- if the new value is of the same type as the old one, value is updated
- if the new value has a different data type, an error is returned

and https://docs.espressif.com/projects/esp ... alue-pairs :
Log of key-value pairs
NVS stores key-value pairs sequentially, with new key-value pairs being added at the end. When a value of any given key has to be updated, a new key-value pair is added at the end of the log and the old key-value pair is marked as erased.

but I still don't know the answer after, can someone help me ? :D

Re: NVS set the same value as stored in nvs partition

Posted: Fri Sep 11, 2020 8:31 am
by ESP_Sprite
You can normally just store the data and it'll overwrite the old value if it's there. Only exception is if the existing value is of a different type: for instance, you do a nvs_set_i8(handle, "myKey", 1) followed by a nvs_set_str(handle, "myKey", "hello world");. As long as you don't do that, you can happily write away your values without checking if there's one already first.

Re: NVS set the same value as stored in nvs partition

Posted: Fri Sep 11, 2020 1:58 pm
by Sweaps
Hello ESP_Sprite,

Sorry, maybe my question was unclear.

What happens if I do this ?

Code: Select all

nvs_open(handle);
nvs_set_i8(handle, "myKey", 1) ;
nvs_commit(handle);
nvs_close(handle);

nvs_open(handle);
nvs_set_i8(handle, "myKey", 1) ;
nvs_commit(handle);
nvs_close(handle);
I save the same value which is 1 on "myKey".
Does the ESP32 overwrite the value in the NVS and resave it ? Or ESP32 understand it is the same value and does not write into the NVS ?

Re: NVS set the same value as stored in nvs partition

Posted: Sun Sep 13, 2020 7:47 am
by ESP_Sprite
Looks like it doesn't write the value if it's the same as is already in flash.