Skip to main content
Answer

Best practices for using virtual currencies

  • September 22, 2025
  • 3 replies
  • 100 views

andreimatei
Forum|alt.badge.img+3

Hello,

I plan to use virtual currencies within my app.

For me, spending or gaining is straight forward and I will use my backend for such calls.

Evaluating the available tokens within the app raises more questions and I tried to understand the official doc here https://www.revenuecat.com/docs/offerings/virtual-currency but I still wonder about some aspects.

Exact use case: I need to know within my view models if the user has tokens or not.

What should I use: Purchases.shared.cachedVirtualCurrencies or Purchases.shared.virtualCurrencies()?

I understand the fact that I need to call the Purchases.shared.invalidateVirtualCurrenciesCache() each time there is a backend transaction, but then what?

Is calling Purchases.shared.invalidateVirtualCurrenciesCache() updating the Purchases.shared.cachedVirtualCurrencies? or do I need to fist call Purchases.shared.virtualCurrencies() and then use the cachedVirtualCurrencies

How about this flow:

  1. App launch call Purchases.shared.virtualCurrencies()  and store it
  2. Use that instance until any spending event
  3. If the user spends/receives tokens, then call invalidate and call again Purchases.shared.virtualCurrencies()

I can’t find a place for cachedVirtualCurrencies in this case 🙈

 

Best answer by Will Taylor

Hi ​@andreimatei 👋

Thanks for using the Virtual Currencies feature! 🙌 Let me see if I can help clear things up.

 

What should I use: Purchases.shared.cachedVirtualCurrencies or Purchases.shared.virtualCurrencies()?

...

How about this flow:

  1. App launch call Purchases.shared.virtualCurrencies()  and store it
  2. Use that instance until any spending event
  3. If the user spends/receives tokens, then call invalidate and call again Purchases.shared.virtualCurrencies()

This is very close to the flow I recommend for most apps! The only suggestion I’d make is to refresh your app’s VC balances by calling Purchases.shared.virtualCurrencies() whenever a view that displays VC balances is displayed. That function will return cached balances if they’re available and still valid, or fetch updated balances from the network if needed. This way, your app always shows the most up-to-date values, which comes in handy if the user’s takes an action on a different device which updates their VC balances.

 

Is calling Purchases.shared.invalidateVirtualCurrenciesCache() updating the Purchases.shared.cachedVirtualCurrencies? or do I need to fist call Purchases.shared.virtualCurrencies() and then use the cachedVirtualCurrencies

When you call Purchases.shared.invalidateVirtualCurrenciesCache(), it clears the locally cached virtual currencies but does not fetch new data. As a result, Purchases.shared.cachedVirtualCurrencies will return nil until you call Purchases.shared.virtualCurrencies() again. That function fetches the latest data from the network and updates the cache on the device.

 

I can’t find a place for cachedVirtualCurrencies in this case

When you call Purchases.shared.cachedVirtualCurrencies , it returns whatever virtual currencies are currently stored on the device, even if the cache is considered “expired”. This can be useful if the device is offline for an extended period and you still want to show balances to the user. That said, many apps won’t need this behavior, so whether you use it depends on how important offline support is for your app.

 

I hope that helps, please let me know if you have any other questions 🙌

- Will

 

This post has been closed for comments

3 replies

Will Taylor
RevenueCat Staff
Forum|alt.badge.img+1
  • RevenueCat Staff
  • Answer
  • September 22, 2025

Hi ​@andreimatei 👋

Thanks for using the Virtual Currencies feature! 🙌 Let me see if I can help clear things up.

 

What should I use: Purchases.shared.cachedVirtualCurrencies or Purchases.shared.virtualCurrencies()?

...

How about this flow:

  1. App launch call Purchases.shared.virtualCurrencies()  and store it
  2. Use that instance until any spending event
  3. If the user spends/receives tokens, then call invalidate and call again Purchases.shared.virtualCurrencies()

This is very close to the flow I recommend for most apps! The only suggestion I’d make is to refresh your app’s VC balances by calling Purchases.shared.virtualCurrencies() whenever a view that displays VC balances is displayed. That function will return cached balances if they’re available and still valid, or fetch updated balances from the network if needed. This way, your app always shows the most up-to-date values, which comes in handy if the user’s takes an action on a different device which updates their VC balances.

 

Is calling Purchases.shared.invalidateVirtualCurrenciesCache() updating the Purchases.shared.cachedVirtualCurrencies? or do I need to fist call Purchases.shared.virtualCurrencies() and then use the cachedVirtualCurrencies

When you call Purchases.shared.invalidateVirtualCurrenciesCache(), it clears the locally cached virtual currencies but does not fetch new data. As a result, Purchases.shared.cachedVirtualCurrencies will return nil until you call Purchases.shared.virtualCurrencies() again. That function fetches the latest data from the network and updates the cache on the device.

 

I can’t find a place for cachedVirtualCurrencies in this case

When you call Purchases.shared.cachedVirtualCurrencies , it returns whatever virtual currencies are currently stored on the device, even if the cache is considered “expired”. This can be useful if the device is offline for an extended period and you still want to show balances to the user. That said, many apps won’t need this behavior, so whether you use it depends on how important offline support is for your app.

 

I hope that helps, please let me know if you have any other questions 🙌

- Will

 


andreimatei
Forum|alt.badge.img+3
  • Author
  • Member
  • September 22, 2025

so, long store short, it’s safe to call Purchases.shared.virtualCurrencies() many times since it’s basically using cached values and I won’t flood the network.

 

thank you


Will Taylor
RevenueCat Staff
Forum|alt.badge.img+1
  • RevenueCat Staff
  • September 22, 2025

so, long store short, it’s safe to call Purchases.shared.virtualCurrencies() many times since it’s basically using cached values and I won’t flood the network.

Yes, that’s right! You can call it as many times as you like, and it will handle the logic of whether to use the local cache or update the Virtual Currencies from the network for you 👍