views:

64

answers:

3

I'm developing an app that processes a constant stream of incoming data from the network and provides a number of different UIViews for the user to view that data.

When certain model data gets updated based on the incoming stream from the network, I access the associated UIViewController or UITableViewController and do -setNeedsDisplay on it (in the case of UIViewController) or -reloadData (in the case of UITableViewController).

Is there a way to check if a given UIView is currently being displayed (beyond just being loaded) so that I only do -setNeedsDisplay or -reloadData if the user is currently looking at that UIView? It would seem that calling -setNeedsDisplay or reloadData on a view that the user is not currently looking at is a waste of processing power and wouldn't be good for battery life. When the user eventually switches over to a view that previously got updated, doing -setNeedsDisplay or reloadData on the -viewWillAppear would make more sense.

Thanks

+1  A: 

it is easier if you are using some sort of navigation structure..such as a UITabBarController or UINavigationController, but if you aren't, something like

if([self.view isKindOfClass:[MyViewClass class]])

might work. or maybe

if(self.view == myView)
Jesse Naugher
I think I understand... I am using a tab bar controller, and a hierarchy of UINavigationControllers under each tab item. The incoming network stream data is received on a separate thread (via NSOperation). When data comes in, I access a singleton that gives me access to all of the various UIViewControllers, and update them accordingly via calls to -setNeedsDispaly or -reloadData. I just need a way of not unecessarily updating a given UIView's screen if the user is not currently on that view.
Paul
so your implementing nonlazy loading, have you thought of updating the data for the view controllers when you VIEW them, instead of everytime new data comes in? its a more efficient and preferred way of doing things.
Jesse Naugher
I currently rely on -viewWillAppear to call -reloadData or -setNeedsDisplay to update the view when the user goes to it (non-lazy loading). However, I need a solution to also update the view if the user is already on a view and fresh data comes in that is meant for it. Right now, I handle this by having pointers to all the view controllers and calling reloadData/setNeedsDisplay every time new data for a view comes in (even if the user is not currently on that view), which is wasteful of processing resources.
Paul
you could keep a pointer to the view you are currently looking at and update it any time you change views, then just check that pointer against the list you have of all the views, to see which one you are currently on when the data comes in...slightly sloppy, but should work.
Jesse Naugher
Thank you very much
Paul
+1  A: 

Add this to your controllers, or to a category of UIViewController. Access it using a property or the variable:

- (void)viewDidAppear:(BOOL)animated
{
 [super viewDidAppear:animated];
 visible = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
 visible = NO;
 [super viewWillDisappear:animated];
}
Peter DeWeese
This is an idea worth pursuing. I think you follow my problem. Thanks.
Paul
+2  A: 

After doing some research, I found this answer in a different question posted on here...This seems to be the best way...

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:

if (viewController.view.window) { // viewController is visible }

Paul