2018-04-20 08:15:40 UTC
I have an app where user can edit data and save to my server. I wonder what’s the best way to update affected view controllers in the navigation stack?
To give an example, imagine it’s an a recipe app where users can create recipes and edit other’s recipes. In the navigation controller’s root view controller, I show a list of all recipes, in each cell, in addition to the recipe name, it also shows the total number of ingredients the corresponding recipe requires.
When you tap a cell, I show the detail of the corresponding receipt. In this detail view, I have a cell that links to a view controller that shows the list of ingredients, and in that view controller, users can tap edit to show a view controller that allows adding/removing ingredients.
This set up means the same data can be displayed across view controllers in the navigation stack, and the it changes, the they need to be in sync.
So the question is, when the ingredients change, what’s the best way to update that ingredient count number in the root view controller?
One more complication is that the list of all recipes come from my server (via json).
I think I have a few choices:
1. Load all data (the list of recipes comes with full details) into Core Data. Basically I create a local replica of my server data and use a NSFetchedResultsController to fetch all ingredients sectioned by recipes. And then show the array count in each section for each cell.
Make the recipe list view controller listen for NSFetchedResultsController change event and when the ingredients change, reload the cells.
But this means I have to deal with core data (really hard to write bug free code), and the initial loading might be slow if the list is huge. Any since I don’t really need to persistent the data locally, I can use in memory store, but I still have to manually purge the store when all recipe view controllers are popped (the root view controller of my app isn’t the recipe list view controller)
2. Server returns tailored data for each view controller. For example, for the recipe list view controller, the server returns a list of recipe containing only names and ingredients counts. For the recipe detail view controller, the server returns details just for the corresponding recipe, and in the ingredient view controller, the server returns a list of ingredients just for that recipe, basically every view controller needs to make a request to my server to get its data.
This means I need to define object types specifically for each view controller, even though the types might refer to objects are conceptually the same. It also means updating the recipe list view controller isn’t that straight forward, because its data has no connection with that in the ingredient list view controller.
One solution might be that when users save the ingredient, I post a message containing the recipe id and the new ingredient count to NotificationCenter, and the recipe list view controller should listen for that message and update accordingly. But this sounds pretty cumbersome.
Which solution do you think is more appropriate or do you have any other better way?
Cocoa-dev mailing list (Cocoaemail@example.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription: