简体   繁体   中英

Get the topVC or Visible VC and check if a specific VC is in the stack

A user adds an item to their bag. As soon as the item is added to the bag, we make a network call to make sure that item is still available.The network call takes a few second to complete in the background.

When an item is added to the cart, it is stored in a singleton that can be accessed from anywhere in the app. Like this:

static let shared = Cart()    
var products = [Product]()

When the network call returns and the product is unavailable, we remove it from the singleton. This causes an issue if the cart VC was opened during the network call because the table view needs to be reloaded. For that reason, we need to check if the VC that is visible is the cart VC and reload the table view. I would also like to check if the CartVC is in memory and reload the table view. Because if the cart VC is in memory and below another VC then it will also have bad data and when the user closes the VC on top, they will see bad data. How would I do that?

Before this is marked as a duplicate, I did check other posts and none of them work very well. There is also a lot of methods to do this and I would like to know which is best in swift 3.

You can use notifications to do it. Imagine the case where the item is not available anymore and the VC is open:

1 - Subscribe to a notification like "ItemNotAvailableNotification".

2 - The network call returned and the item is not available anymore.

3 - Post a notification "ItemNotAvailableNotification".

4 - In your VC handle the notification.

Also this approach allows you to handle the "bad data" in your "CartVC" and your "PreviousVC" where the user were lead to believe that the item was available.

When the cart is down in the stack make use of viewWillAppear and reload the table then. That way you don't reload the table more times than is needed. (If the cart is in the stack and four different network calls come back you only reload the table right before it appears rather than four times when it is hidden.) Table views are often reloaded in viewWillAppear specifically to deal with potentially stale data.

For the case when the cart is on screen your singleton can send a notification whenever a product comes back as unavailable and the cart can register for that notification in viewWillAppear and deregister in viewWillDisappear (or viewDidDisappear ). The notification could either trigger a complete reload of the data or you could include which product isn't available and let the user know what happened (rather than something suddenly disappearing from the cart with no explanation).

This way the singleton doesn't need to know anything about the cart view controller allowing it to be more reusable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM