简体   繁体   English

获取topVC或Visible VC,并检查堆栈中是否有特定的VC

[英]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. 如果在网络调用期间打开购物车VC,这会导致问题,因为需要重新加载表视图。 For that reason, we need to check if the VC that is visible is the cart VC and reload the table view. 因此,我们需要检查可见的VC是否是购物车VC,然后重新加载表视图。 I would also like to check if the CartVC is in memory and reload the table view. 我还想检查CartVC是否在内存中并重新加载表视图。 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. 因为如果购物车VC位于内存中并且位于另一个VC之下,那么它还将包含错误数据,并且当用户在顶部关闭VC时,他们将看到错误数据。 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. 还有很多方法可以做到这一点,我想知道哪种方法最适合快速3。

You can use notifications to do it. 您可以使用通知来执行此操作。 Imagine the case where the item is not available anymore and the VC is open: 想象一下,该项目不再可用并且VC已打开的情况:

1 - Subscribe to a notification like "ItemNotAvailableNotification". 1-订阅类似“ ItemNotAvailableNotification”的通知。

2 - The network call returned and the item is not available anymore. 2-网络呼叫返回,该项目不再可用。

3 - Post a notification "ItemNotAvailableNotification". 3-发布通知“ ItemNotAvailableNotification”。

4 - In your VC handle the notification. 4-在您的VC中处理通知。

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. 另外,这种方法还允许您处理“ CartVC”和“ PreviousVC”中的“不良数据”,在这些情况下,用户被认为该项目可用。

When the cart is down in the stack make use of viewWillAppear and reload the table then. 当购物车在堆栈中时,使用viewWillAppear并重新加载表格。 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. (如果购物车在堆栈中,并且有四个不同的网络调用返回,则只在表出现之前重新加载表,而隐藏表时才重新加载。)表视图通常在viewWillAppear重新加载,专门用于处理潜在的过时数据。

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 ). 对于购物车在屏幕上的情况,只要产品viewWillAppear ,您的单身人士就可以发送通知,并且购物车可以在viewWillAppear注册该通知,并在viewWillDisappear (或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. 这样,单例无需了解有关购物车视图控制器的任何信息,从而使其更具可重用性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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