Search code examples
iosswiftin-app-purchasetableview

How to reloadData after an in app purchase through modalView


I've looked at different sources to find an answer to this and possibly tried several ways to reloadData() but no luck so far.

Here is my case:

I have a tabBar where I present the content in a tableView. When you go into that tab, I present a modal view if you are not purchased the content yet.

enter image description here Let's say you have purchased the content. The modal view dismisses itself but tableview doesn't show anything unless you kill the app and re-run again. When you this, tableView reloads itself with the content. No problem there. But this is definitely problematic and I do lose users just because of this.

I do keep track of the purchase with a key like this:

private var hiddenStatus: Bool = UserDefaults.standard.bool(forKey:"purchaseStatus")

I set it to true on a successful purchase as shown below:

self.setNonConsumablePurchase(true)

And finally the code where I check if the key is set to "true" on purchase. I call this on the viewDidLoad but it doesn't make sense because it's been already called when user touched on this tabBar item.

if hiddenStatus == true {

    DispatchQueue.main.async(execute: {

        self.segmentioView.valueDidChange = { [weak self] _, segmentIndex in

            switch self?.segmentioView.selectedSegmentioIndex {
            case 0:
                self?.observeAugustProducts()
                self?.tableView.reloadData()
            case 1:
                self?.observeSeptemberProducts()
                self?.tableView.reloadData()
            default:
                self?.tableView.reloadData()
            }
        }
    })
 }

So I'm looking for two things:

1- To keep everything as it is and tweak the code to make this work.

2- Alternative ideas to give users a better in-app purchase experience (I once used UIAlertViewController for in-app purchase but decided not to go with it)


Solution

  • I recommend using this extension:

    extension NotificationCenter {
        func setUniqueObserver(_ observer: AnyObject, selector: Selector, name: NSNotification.Name, object: AnyObject?) {
            NotificationCenter.default.removeObserver(observer, name: name, object: object)
            NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: object)
        }
    }
    

    Before dismissing your Modal View, have your Modal View's ViewController post a Notification.

    Something like:

    NotificationCenter.default.post(name: "Purchased", object: nil)
    

    Then have your TableView's ViewController register to listen for that Notification (in viewWillAppear) - and handle it.

    Something like:

    NotificationCenter.default.setUniqueObserver(self, selector: #selector(handlePurchased), name: "Purchased", object: nil)
    

    and

    @objc func handlePurchased() {
        // Reload your TableView, etc.
    }
    

    Hope this helps someone!