Is it safe to call Purchases.login multiple times as long as the id is consistent?

  • 19 December 2021
  • 3 replies

Badge +2

Are there downsides to calling Purchases.shared.login(<id>) several times?

I don’t load the user id until the user has hit a shared TabBarController inside the my app after logging in through a third party. This call is reused whenever a user refreshes data or reopens the app.

I’d like to call Purchases.login whenever user data is loaded. This can happen several times during a session, although typically it only happens once.

This would be the simplest way to fill ids. I think it would also backfill existing users who won’t go through a login flow again.



Best answer by sharif 20 December 2021, 21:09

View original

3 replies

Userlevel 4
Badge +9

RevenueCat will work as intended if you call login with the same app user ID multiple times. The SDK tries to be intelligent about when to send identification requests, and in this case calling login with the same app user ID shouldn’t result in multiple API requests.

However, you may run into other issues with this approach. For example, you can introduce bugs if the app user ID isn’t available for one of the calls, causing you to accidentally pass in a null value which signifies to RevenueCat that the user is anonymous. There could potentially be race conditions as well - you could pass in null, then realize the app user ID is available and pass that in on the next call, causing needless purchase transfer operations in RevenueCat and resulting in extra events being sent. If you do go this route, be careful not to cause bugs like these, as they are difficult to debug and present themselves as serious UX issues like getting locked out of entitlements.

The best approach is to call login only when there are changes in identity. That means calling it on login, logout, app install, etc.

Badge +2

Thanks for the response Sharif -

That makes sense to me. I can add a peek for the user id during login that will let me identify.

Still, I’m not sure what to do about backfilling existing users. It’s not a priority since the app is not that big and many users have already churned, but I’m still curious if RC has any guidance on backfilling identity for users who are already logged into the app?

Userlevel 4
Badge +9

Sounds like you’re migrating an existing app to RevenueCat? It’s common in migrations to run code when a user updates. For example, you run some code when the app is first launched after your RevenueCat update, then set a value in UserDefaults to signify that the update has already been handled so you don’t call that code for the same installed app in the future. So something like:

// applicationDidFinishLaunchingWithOptions
if (hasLaunchedWithRevenueCat == false && user.isLoggedIn) {

I think it’ll depend on your specific use case so definitely feel free to share more info here, but generally that’s the strategy we recommend.