简体   繁体   中英

Dismissing view in UINavigationController and at the same time syncing

There's two views on a NavigationController stack:

  1. EDIT ITEM-DETAILS VIEW (= basically a form)
  2. SHOW ALL ITEMS IN A TABLE VIEW

I wonder if there are best practices for the task I have:

When the user taps "BACK" in the UINavigationController-bar (while being in view 1 ) the app should update the item on the server.

That's not so difficult, but the BACK-action leads to view 2 , and 2 is not up-to date, because the update happened in the background and wasn't through before the GET-request for the table view data finished.

So in order to have view 2 always show accurate data, I have several options. All a bit annoying.. (for example having ViewController of view 2 talk to server on 1 's behalf and update itself when completed, or having a "update happened" notification that triggers a reload, ...)

But.. what's a good best-practice for this case?

I think I would make a central place for the Items. Lets call it a ItemsStore. ItemsStore is a singleton that has the responsibility to have a set of the latest items and give access to the items. It also fires notifications if new data arrives, or old data is saved.

In this case:

  • View 2 adds the data to the store. The store notifies that there are changes.
  • View 1 updates on the notification.
  • View 2 also asks the ItemsStore to save the data to the server.

I would not give the responsibility of loading and saving to the controllers, it will get ugly and complex.

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