Quantcast
Channel: Switching between view controllers that are loaded in the background - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 2

Switching between view controllers that are loaded in the background

$
0
0

I have a dining menu app that scrapes the data from a website and redisplays it in a mobile format, displayed below:

enter image description here

If the user swipes left and right, the app will show the previous/next meal (ex. if current meal is lunch, swiping left will show dinner).

I'm trying to come up with a more efficient way of switching between menus. With my current implementation, each menu is a separate view controller.

When someone swipes right, it calls:

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer {    //swipes to next meal    MealType curMeal = currentMenu.type;    int newMeal = [MenuLoader MealAfterMeal:curMeal];    MenuTableController *newMenu = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];    newMenu.currentMeal = newMeal;    [self.navigationController pushViewController:newMenu animated:YES];}

When each view controller is created (including the first), it loads the menu in a background thread:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        MenuLoader *ml = [[MenuLoader alloc] init];        currentMenu = [ml mealForType:_currentMeal Specificity:[self summaryPreference]];        dispatch_async(dispatch_get_main_queue(), ^(void) {            ...        });    });

Inside the mealForType:Specificity: call, I parse 2 separate web pages through NSURL, and combine the information from both sites:

NSURL *url = [NSURL URLWithString:urlString];NSData *pageData = [NSData dataWithContentsOfURL:url];

Some of the optimizations I'm hoping to make are:

  • Preloading adjacent menus in another thread to reduce loading time when someone swipes
  • If someone swipes forward and then back, it should show the already loaded menu. Right now, it makes a fresh call to the server and reloads everything.
  • Right now, if someone swipes multiple times in a row before any of the menus finish loading, the app tries processing all of the server calls at once and this slows down the loading time. If someone does swipe repeatedly, the current menu should take precedence, and possibly the other calls should be terminated.

It sounds like a big overhaul of my design, so I was wondering if there was a better approach to loading menus than what I currently implemented.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>