I'm building an iPhone app which gets updates from the web at regular time intervals and when the user is browsing through the apps data.
I have a class called updateManager which handles the updating process.
When the updating process is finished (0.1-3.0 seconds later) the data in my app could be changed but the user is still viewing the old (false) data. This causes big problems when the user is viewing a (drill-down)tableview and he selects a cell which data doesn't exist anymore.
Is there a way to access the current view(controller) from another class and reload it's (table)view? It is not possible to send the current view controller to the updateManager since the user could have changed view's while the update is in progress
EDIT:
I looked at this post: Reload a tableView from the AppDelegate but i think the problem with the user switching views while updating is still there isn't it?
Not sure that's the best solution. You get a more decoupled design if you use the Observer pattern instead. Use NSNotificationCenter to post a notification when the data changes. Then any view who is displaying data would subscribe to those notifications and can then reload the table upon such an event.
i would fire a NSNotification with the NSNotification Center
Your ViewController with the TableView listen to the Notification (and reload your table view if the notification fired) and your update Class fire the Notification if the data changed.
Related
I am working on an IPhone app that, through viewDidLoad, makes a connection and pulls data into a table. I would like the controller, or at least the table, to reload every time the controller is displayed, even if someone just switched from one tab to another or closed and re-opened the app on this controller. Is there a way to do this? I can't seem to find anything, it's also kind of a hard thing to search for. I have found discussions of when to put code in viewDidLoad but not another method that is run every them the controller displays.
Thanks,
Cheryl
You want to use
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//code to reload table viewdata
}
Each time the view appears this method is called. I use this a lot in my app.
There's no way to do it by injecting code in only one place.
You can think of a workaround, like for example sending custom "reload request" notification from required parts of code:
viewDidAppear: in your view controller
applicationDidBecomeActive: (if table is presented)
tabBarController:didSelectViewController: (if switched onto controller with the table)
tabBar:didSelectItem: (same as above)
etc.
In you view controller simply observe this notification and reload data when required.
Although, whats more important: do you really need to reload data under such harsh requirements? In most cases data reload happens when
view controller's viewDidLoad: is called
it is manually initiated (button, for example)
long time passed since the last update was received
Otherwise it's just an overkill.
For a simple note application, there is a table view controller with a fetchedResultsController that has a list of saved notes in core data. The user can either click the + button to add a new note or click on one of the records in the table view to edit an existing note. In either case, a modal view controller with a text view shows up.
I want to save as the user types each letter, so in textViewDidChange I am updating Core Data. If the user types too fast, the core data saves hang. I recently added code to add the updates to NSOperationQueue which was an idea taken from here:
How to implement work queue in iOS where only newest request is handled? and that has helped somewhat.
Here's the problem I need help with. Let's say the user types an update in the textView, then pushed Done to return to the table view controller, and then clicks on the same note to edit the note again before the original update was refreshed in the table view. The user is presented with a textView that has the old data. The update is not there. If the user goes back to the table view and edits the note again, the latest updates show.
What are some approaches to consider, or how could I attempt to fix this issue? I want the textView to always show the latest updates even if user is switching back and forth from the textView and tableViewController faster than a user should ;)
You don't need to save the context everytime the user adds a character, do it only when he finishes adding the note, or at least when the textfield loses its focus (first responder resigns). There's really no point in doing it everytime he taps. Saving the context is a pretty expensive operation.
Maybe try saving all of the changes at once in:
- (void)viewWillDisappear:(BOOL)animated
Instead of constantly saving as the user types, which is a bit redundant.
If the user goes to their home screen, then save the changes in
- (void)applicationDidEnterBackground
I have a simple iPad application with 5 views. On the first view, the user is asked to make some selections and set some options. From this information, the other 4 views are programatically changed after an NSNotification message is sent to them. (i.e controls are added, updated).
My problem is that when the application is first loaded, the user sees View1, but View2, View3, View4 and View5 have never been opened yet, so any changes I make programatically to those views are not done and when the user navigates to them (via the tab bar) for the first time, no changes are shown.
[EDIT: I should point out that the code for making the changes to each view is contained within the ViewController itself, and is executed when the view observes the incoming NSNotification. When the view is not loaded, it understandably never received the incoming NSNotification.]
Only after the user looks at any of those screens at least once and then goes back to View1 and makes changes, are the other Views updated properly.
I thought I could get around this issue by actively loading Views 2,3,4 and 5 into memory on application start, so that they are ready to begin receiving notifications right away.
Is there an easy way to do this in iOS 5?
Why do the view changes straight away?
I would store an indicator of the changes needed when the users answers the questions on the first view and then apply the changes on -viewDidLoad of each view that needs to be changed.
Instead of trying to load the views into memory, I'd suggest you initialize these views with the options that the user set on the first view. What I usually do in such situations, when I have a global parameters that are used in many places, I create a utility class to keep the data, make it a singleton, then access the shared instance in the viewDidLoad in the views that use the data during initialization.
I am making an app which basically presents data (from plist) to user in table form. I have a lot of controllers and the user basically navigates through these controller to get to an item after which a final detail display is shown.
At that last view, the user can add that to favorites.
The way that I am passing data to view controller is that I pass them specific data, so at the last view, if the user adds the item to favorites, I write that data to file. But how can I tell all my other controller that the data has updated?
This is more of a design problem and I don't know where to go..I have some idea but I want to know what is the standard way of doing this...Thanks.
If you're going to be listening for events within multiple objects, take a look at NSNotificationCenter. You can simply subscribe to listen for notifications then post one once the data is updated.
NSNotificationCenter Class Reference
I´m building an app which simulates an Inputstream of information from a sensor. When information are detected and parsed I want to send Notifications, that will be observed
by the different views which update themselves to a given timeintervall.
Is its MVC compliant to use Notifications to get a connection from Model/Controller to the View?
I´m asking because I´m often reading about Views sending Notifications to the Controller when a User interacted with the UI.
Thanks
The view is supposed to be just a view and should not have to receive notifications to update itself. I am not saying I have not done this myself as it work having the view update itself. I would recommend that you design your application so that your view controllers receive the notifications and update the views appropriately. Use your views to display and interact with the user sending any interaction updates to your view controller and the view controller sending any display updates to the view.