Say I have 3 records, then I delete them and collect the garbage. If I try to save a new record again, it will be saved with a record ID 4 , but it should be back to 1 after the delete.
EDIT: The indexes are actually going crazy, if you start your program by erasing memory and then saving a new record after it, and you run this multiple times, your writing record ID for every save would go :
ID=1
ID=3
ID=5
ID=7
instead of staying at 1 all the time , because you delete and collect every run before you save.
delete :
#define FILE_ID 0x1111
#define REC_KEY 0x2222
fds_record_desc_t record_desc;
fds_find_token_t ftok ={0};
ftok.page=0;
ftok.p_addr=NULL;
// Loop and find records with same ID and rec key and mark them as deleted.
while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
{
fds_record_delete(&record_desc);
}
fds_gc();
I save again only after the handler result :
static void fds_evt_handler(fds_evt_t const * const p_fds_evt) {
...
...
case FDS_EVT_GC:
if (p_fds_evt->result == FDS_SUCCESS)
{ (*flashDelegate)("garbage collected !"); } // ** this will happen and here i save
case FDS_EVT_DEL_FILE //this will not happen but it will delete the file
...
So what happens is that when I save after the delete, he start saving from record ID >1 .
Save with this :
#define FILE_ID 0x1111
#define REC_KEY 0x2222
fds_record_t record;
fds_record_desc_t record_desc;
fds_record_chunk_t record_chunk;
// Set up data.
record_chunk.p_data = &toSave;
record_chunk.length_words = (sizeof(toSave)+3)/4;
// Set up record.
record.file_id = FILE_ID;
record.key = REC_KEY;
record.data.p_chunks = &record_chunk;
record.data.num_chunks = 1;
ret_code_t ret = fds_record_write(&record_desc, &record);
I even tried to init it before write with this :
record_desc.record_id = 1 ;
but it's still different from 1 even after the delete and the gc.