Data not reflect correctly

Given the code below

use ic_cdk::storage::stable::StableBTreeMap;

static STORE: StableBTreeMap<String, String> = StableBTreeMap::new();

#[ic_cdk::update]
fn update_and_get(key: String, new_value: String) -> String {
    // Update the value
    STORE.insert(key.clone(), new_value);
    
    // Retrieve and return the updated value
    STORE.get(&key).unwrap_or_else(|| "Not found".to_string())
}

When I insert a data and update via update_and_get some how the STORE.get return the old value instead of lastest update data. After few seconds it will update correctly

Can anybody explain why this happened?

1 Like

What version of ic-cdk is this? I don’t see anything referring to ic_cdk::storage::stable in recent docs

Sorry I copy example from kapa ai

correct code is using this

use ic_stable_structures::{DefaultMemoryImpl, StableBTreeMap};

version
ic-stable-structures = “0.6.5”

After few seconds it will update correctly

What do you mean by this? How are you testing your code?

My project kinda complex to read but here is the code for testing

Test Code where the logic is test for update

The issue happened in 3.3 when I testing this in PicJs and staging version of the app (deployed in mainnet).

  • Record updated by the logic in 3.1
  • 3.2 return the old data

I have to wait more than 5 second for polling to make sure the record updated and re-render the state

I don’t understand the connection of your linked code with the snippet above. If your linked code behaves as described, then I would first suspect some ordering issue with asynchronous requests. Can you rule that out?

If you truly have a write and subsequent read in the same message, as in the snippet, I would be very surprised to see the described behaviour. Can you actually reproduce the issue in a minimal example with the code from the snippet?

You’re right. I try to make a minimal example, but cannot reproduce the issue