Proper way to wait until prepare is done
See original GitHub issueVersion of react-native-iap
0.3.23
Platforms you faced the problem (IOS or Android or both?)
Android
Problem
Some methods of this library are getting called before prepare()
is done.
Description
I’m wondering what the recommended way is for waiting until the library is ready to process requests on Android. The example provided in the README cannot really be applied directly to my application, since what I’m doing is I’ve created a class with static
functions for handling In App Purchases (kind of an abstraction on top of this library). In my App.js
I call prepare()
in componentDidMount()
, and so the issue is that the other component of my app will load and call getProducts(itemSKUs)
before componentDidMount()
(and therewith prepare()
) is done executing.
My current (not very pleasing) solution is to just set a timeout of 1 second in the other component before calling getProducts(itemSKUs)
from it.
I had an idea about setting a boolean like this and then making it so that the other functions have to wait for it to be true before they can execute, but I’m not sure how to accomplish that.
export default class IAPManager {
static hasUnlockedPremium = undefined;
static productInfo = undefined;
static isReady = undefined;
static async prepare() {
const isConnected = await NetInfo.isConnected.fetch();
if (isConnected) {
try {
await InAppPurchase.prepare();
IAPManager.isReady = true;
} catch (e) {
console.log("Could not prepare IAP", e);
IAPManager.isReady = false;
}
} else {
IAPManager.isReady = false;
}
}
...
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:18 (8 by maintainers)
@dooboolab thanks I’ll follow your advice 😃
@axelkennedal I exactly agree with what you’ve described above. I just posted the starter solution for your problem. It could be improved, like when you call
getProducts
and get failure message you can call it again after few seconds settingtimeout
method. Also, usingmutex
must be a better solution and for sure I should manage this in the native side. Will come back to you if I’ve done it.