question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper way to wait until prepare is done

See original GitHub issue

Version 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:closed
  • Created 5 years ago
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
nico1510commented, Jun 2, 2018

@dooboolab thanks I’ll follow your advice 😃

1reaction
hyochancommented, May 10, 2018

@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 setting timeout method. Also, using mutex 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

7 Ways God Uses Waiting to Prepare You - Crosswalk.com
Waiting is a prime time to slow down and look back at the powerful things God has recently done in your life and...
Read more >
Fine Dining Etiquette for Servers - WebstaurantStore
Clear From the Right - Always clear from the guest's right side and follow a clockwise order around the table. Don't Stack Dishes...
Read more >
Tips to Prepare Food in Advance for Parties | Premio Foods
Opt for recipes with short cooking times and minimal prep to make the best use of your time. Write out every component of...
Read more >
9 Ways We Are To Wait Upon the Lord - Grace Covenant Church
We simply need to wait until the day the Lord returns to take us all there together to be with him. What a...
Read more >
Food safety when cooking - Better Health Channel
Wait until the standing time is over before you check that the cooking is complete. Food continues to cook even after the microwave...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found