Update previous views when editing current view - ios

In short, here is my problem:
I go from view controller A to VC B
Then from VC B to VC A.
Loop 1 and 2 for a number of times. All by pushing.
Now I edit VC A (e.g. Liking one of my post) and save. Then I go back by tapping on Back button. I want to see the changes in VC A (e.g. The number of likes increase 1 in my post)
That is the simple case when I only have 2 VCs. You can have many VCs in the loop, and you can may want to update other VCs not in the loop as well.
One example is in social network app like Instagram, where you can go to your profile, followers list, go to sb's profile, go to their follower list, go to your profile from there..so on and so on. Then at the end you make a like of one post, and you want to update view in Home tab, as well as all views in middle of the loop, while you tapping on Back button.
I know we can implement in 2 ways:
In each VC, in viewwillappear we check for update and then update. The disadvantage is that it will not work in offline mode. And it is bulky to check everywhere.
Use notification. One view having changes will notify other views to update. But you have to define by yourself which views will accept which types of notification, and having multiple notification triggered in your app is quite messy and hard to manage.
Are there any other ways? How are you doing in your app?
My question is more of asking about architect, not for coding sample, so please answer by giving a solution, an architecture and some short analysis of the result of it.
This question is for both ios and android.

My suggestion is
Save the number of likes and the post id in NSUserDefaults and check in each view controller while your post id is same as the value in NSUserDefaults the increment it by your count of like for the post in NSUserDefaults . But at some point of time you have to sync it with webservice .

Related

Passing data to Detail View, actual data or reference ID

Situation
Let’s say there is an iPhone app that shows articles. Articles are loaded from a server.
It has two views.
TableView: Shows list of articles
DetailView: Shows detail of selected article
Problem / Question
I want to know better way of passing data from TableView to Detail View.
Which one of the following is a better practice?
Option 1
Pass an actual Article object from TableView to DetailView
DetailView just displays the Article
Option 2
Pass reference ID of Article from TableView to DetailView
This case, the DetailView loads the article from server by the ID of the Article.
Option2 seems a better design since relation between TableView and DetailView is minimum.
Option1 seems a little bit faster since i doesn’t have to connect to API ever time it loads an article
I understand it depends on situation but i would like to know if there is any reasonable guideline.
You should use a combo of both since many apps such as Facebook also do the same. For eg. if I have a photo that has likes in my notification bar, I can click on that photo and see it even if I do not have an active connection at that time. I will obviously be shown the older data. However at that time, Facebook immediately sends a call to the server and the updated likes are shown.
Therefore it should be ideal to pass the entire object to the next VC and immediately send an async call to the server. Any changes should then be reloaded in your complete data model. Hope this helps :)

iOS app need to design a flexible and elegant way to pop surprise gift messages to users in different places within my app

I am building an iOS app with the following view controllers:
1) ProductViewController - displays a carousel of products. When the user rotates to a product, the user can pick a button to see details of the product in view or another button to add the product to a shopping cart
2) HistoryViewController - displays detailed history about the products on a vertically scrolling timeline (Implemented using UIScrollView).
3) GameViewController - initiates a game where the user can play a trivia game where he scrolls through a set of views using back and forth arrow buttons (there are about 20 of these)
OK so much for the setup... here is the crux of my design problem:
I want to create popup messages for the user when the user gets to certain key points in the app. Lets call these key points "anchor points"
So for example the popup appears as soon as the user sees the 3rd product in the carousel view (i.e. in ProductViewController)
The popup appears again when the user gets halfway down the HistoryView (in HistoryViewController)
The popup appears again when the user gets to sees 7 items in GameViewController.
I want to add these "anchor points" in a flexible manner so I can easily change those places in the app where the popup appears (with minimal code change and mainly through config)
Q1) What would be the best way or design pattern to use to go about this design?
Q2) As a later enhancement I want to use some of the anchor points as key anchor points where if a user has gone through a certain number of key anchor points I can send the user a special message.
Thanks in advance for your help!
I'd probably think about having a separate controller (maybe a singleton) to own the knowledge for this. It deals with any configuration and the number of triggers required before any message is shown.
The other controllers in the app shouldn't know about counts or what things are being monitored for, so notifications are a good option. Define a set of notifications (for the types of events which go into making your anchor points) and have all of your controllers post the notifications as events happen.
Your separate controller deals with observing the notifications, counting and displaying the messages. If the display is as alerts or modal display it's easy. If not, the controller should have a link to the root view controller so it can get the current top controller (alert or modal is preferable).
Here is my answer, hopefully I understood the question correctly and this helps.
Create a popup manager class and initiate where ever it needs to be created first(app delegate?) to keep track of the user progression, and have the popup manager class pop alert messages to let user know of the surprise you have for them.

Dismissing view in UINavigationController and at the same time syncing

There's two views on a NavigationController stack:
EDIT ITEM-DETAILS VIEW (= basically a form)
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.

Is there a simple way to let another ViewController know that a button has been pressed?

I have an app that requires that a menu changes in view controller 1 when a button in view controller 2 is pressed. What is the best way to achieve this?
I've heard a lot of talk about NSNotification but I thought that was for displaying alerts?
The "right" way to do this is to write the new state into the app's data model. When another view controller becomes active, it should update its view according to what the model says. That way, the information will be available to other view controllers even if they don't exist when the user makes the change.
Notifications are a great way to convey information to other objects without having to know about them specifically, but a notification is only effective if the objects that care about it exist at the time that it's sent.
You're thinking about this in the wrong way. One view controller shouldn't care about what happens in another view controller.
If a button being tapped results in changes to the contents of a menu, it sounds like you're changing the data. The button press should tell the model layer that the available options have changed, and the other view controller should load the available options into the menu from the model layer.
I've heard a lot of talk about NSNotification but I thought that was for displaying alerts?
No, it is for distributing information about events to the rest of your application in a way that doesn't couple those parts together. It's not about interacting with the user.
NSNotification is one way to do it and no, it has nothing to do with alerts.
It works like this: a "producer" can post (send) a notification. Other objects can subscribe to notifications and react to the notification. It's an excellent way to decouple objects (the goal is often to make each object know as few information about the other as possible).
Search for NSNotification tutorial, there are quite a few. You should really get familiar with them, they are used a lot on iOS and Mac OS X development as they are very, very handy.
If you're not presenting both viewcontrollers at same time and you transition from viewcontroller1 to viewcontroller2 you could use segues to pass information from vc1 to vc2. I think notifications are great but i think isn't necessary.

Where should I put the logic to get the record from Database?

I have two buttons on a action sheet, when I click one of the button, it will display all the students record in another view. All these records need to be retrieved from DB. Currently I put the logic of retrieve the record in the viewwillAppear method, But after I click the button the screen froze there for a few seconds then the student list will displayed.
Per my understanding my logic of retrieve the record was in viewWillAppear method in student list view. After I click the button , it should to directly to student list view , then in the student list view it will try to load the data. But now after I click the button why it froze in the action sheet? Or are there any other place that I can put the load record logic in so that it will not froze in the previous action sheet After I clicked the button.
If you're going to post to SO, you always should include your existing code. I'd suggest you update your question accordingly. See the FAQ.
When you say that it "froze", do I infer that you're saying that there was a delay before you saw your new view with the data, but that it eventually appeared? If so your task is how to identify where the delay took place. So, I'd suggest you start inserting NSLog statements in the various methods so you can identify where exactly the delay is taking place. Did it take a long time for the new view to be loaded? For the data to be retrieved? Etc. Until you narrow down the problem, identify the source of the delay, you won't be able to solve it. You need to develop the skills to diagnose and troubleshoot your problems. By the way, if you're still having problems figuring out how to diagnose your code and the NSLog doesn't help, you can also refer to Apple's documentation on debugging and stepping through your code. You should only post here after you've narrowed down precisely where in your code the problem is manifesting itself.
Third, most people load their data for their new views in viewDidLoad, not viewWillAppear; if you ever went to a subview of your student list view and then came back to your student list (i.e. the student list view reappeared, i.e. viewWillAppear will trigger again), would you really want to load the data again, even though you've already loaded it? Probably not. I know you might not have another view that you're loading after you load your student list view, but you may eventually (e.g., a student detail view), so good practice is that you should load your view's data in viewDidLoad (which will trigger only the first time the view is initially loaded).

Resources