Skip to content

Commit fe42564

Browse files
authored
Include nvs_commit() on three methods
In [Preferences.cpp](https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/src/Preferences.cpp), the functions: ``` Preferences::clear() Preferences::remove() Preferences::end() ``` should be revised to include a call to `nvs_commit()` as required per [Non-volatile storage library](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html) when using ``` nvs_erase_all() nvs_erase_key() nvs_close() ```
1 parent 21947eb commit fe42564

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

libraries/Preferences/src/Preferences.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ bool Preferences::begin(const char * name, bool readOnly, const char* partition_
5353
return true;
5454
}
5555

56-
void Preferences::end(){
56+
void Preferences::end(){ // modified to add an nvs_commit()
5757
if(!_started){
5858
return;
5959
}
60+
esp_err_t err = nvs_commit(_handle); // to undo changes: delete the lines from here...
61+
if(err){
62+
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
63+
} // ... to here.
6064
nvs_close(_handle);
6165
_started = false;
6266
}
@@ -65,7 +69,7 @@ void Preferences::end(){
6569
* Clear all keys in opened preferences
6670
* */
6771

68-
bool Preferences::clear(){
72+
bool Preferences::clear(){ // modified to add an nvs_commit()
6973
if(!_started || _readOnly){
7074
return false;
7175
}
@@ -74,14 +78,20 @@ bool Preferences::clear(){
7478
log_e("nvs_erase_all fail: %s", nvs_error(err));
7579
return false;
7680
}
77-
return true;
81+
// return true; // to undo changes: uncomment this line and...
82+
err = nvs_commit(_handle); // ... delete the lines from from here...
83+
if(err){
84+
log_e("nvs_commit fail: %s", nvs_error(err));
85+
return false;
86+
}
87+
return true; // ... to here.
7888
}
7989

8090
/*
8191
* Remove a key
8292
* */
8393

84-
bool Preferences::remove(const char * key){
94+
bool Preferences::remove(const char * key){ // modified to add an nvs_commit()
8595
if(!_started || !key || _readOnly){
8696
return false;
8797
}
@@ -90,7 +100,13 @@ bool Preferences::remove(const char * key){
90100
log_e("nvs_erase_key fail: %s %s", key, nvs_error(err));
91101
return false;
92102
}
93-
return true;
103+
// return true; // to undo changes: uncomment this line and...
104+
err = nvs_commit(_handle); // ... delete the lines from from here...
105+
if(err){
106+
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
107+
return false;
108+
}
109+
return true; // ... to here.
94110
}
95111

96112
/*

0 commit comments

Comments
 (0)