Skip to content

Update Preferences.cpp to include nvs_commit() on three methods #4972

Closed
@beamholder

Description

@beamholder

In 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
when using

nvs_erase_all()
nvs_erase_key()
nvs_close()

Suggested changes:

void Preferences::end(){                                         // modified to add an nvs_commit()
    if(!_started){
        return;
    }
    esp_err_t err = nvs_commit(_handle);                         // to undo changes: delete the lines from here...
    if(err){
        log_e("nvs_commit fail: %s %s", key, nvs_error(err));
    }                                                            // to here.
    nvs_close(_handle);
    _started = false;
}
bool Preferences::clear(){                                  // modified to add an nvs_commit()
    if(!_started || _readOnly){
        return false;
    }
    esp_err_t err = nvs_erase_all(_handle);
    if(err){
        log_e("nvs_erase_all fail: %s", nvs_error(err));
        return false;
    }
    // return true;                                         // to undo changes: uncomment this line and...
    err = nvs_commit(_handle);                              // delete the lines from from here...
    if(err){
        log_e("nvs_commit fail: %s %s", key, nvs_error(err));
        return false;
    }
    return true;                                            // to here.
}
bool Preferences::remove(const char * key){                 // modified to add an nvs_commit()
    if(!_started || !key || _readOnly){
        return false;
    }
    esp_err_t err = nvs_erase_key(_handle, key);
    if(err){
        log_e("nvs_erase_key fail: %s %s", key, nvs_error(err));
        return false;
    }
    // return true;                                         // to undo changes: uncomment this line and...
    err = nvs_commit(_handle);                              // delete the lines from from here...
    if(err){
        log_e("nvs_commit fail: %s %s", key, nvs_error(err));
        return false;
    }
    return true;                                            // to here.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions