0

I'm going to try to put as much detail as possible in this post because its very important i accomplish this task and i dont know why im getting these strange results. So, here it goes.

I have 5 view controllers/.xib files. I also have appdelegate and a LocationManager singleton class, designed to provide each .h/.m file with location updates. View controller 1, known as WelcomeView, is the delegate for location updates. So it has the didUpdateLocations method in it. It watches for a person to enter a specific area. When this specific area is entered, it tells the person to go to another area to receive a prize of sorts. didUpdateLocations monitors ALL movement, including watching for the person to enter both area 1 and area 2. If the person successfully enters area 1, it's supposed to switch views to TransitionViewController for 3 seconds, then switch to TimerViewController. Here's the code i used to switch:

self.transitionView = [[TransitionViewController alloc] initWithNibName:@"TransitionViewController" bundle:nil];
[self.view addSubview: self.transitionView.view];

then in the transition view controller, i have it sleep for 3 seconds and then switch to timerviewcontroller, like this:

sleep(3);
self.timerView = [[TimerViewController alloc] initWithNibName:@"TimerViewController" bundle:nil];
[self.view addSubview:self.timerView.view];

When i actually enter the area and all of this is supposed to take place, originally it worked like a charm. As it turns out, it only worked when the phone was plugged into the mac and running from Xcode. Now, I've gotten it to the point that it just takes 45 seconds - 1 minute to actually switch to transition, and then when in transition it takes an additional 20 seconds or so before switching to timer. I've tried various things like putting both commands on the main thread, like so:

sleep(3);
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
    NSLog(@"Finished sleeping; switching to timer now.\n");
    self.timerView = [[TimerViewController alloc] initWithNibName:@"TimerViewController" bundle:nil];
    [self.view addSubview:self.timerView.view];
}];

or putting the transition to timer on a background thread, like this:

[self performSelectorInBackground:@selector(transitionToTimer) withObject:nil];

I'm at a loss. I don't know why this is happening and this job was supposed to be done last week. Any suggestions for me? Anybody?

EDIT

This is the strangest part of it: If i start the app while already inside area 1, it runs through flawlessly. The app does exactly what its supposed to. It's when i walk into the hotspot that it crashes or takes forever to switch views. So if the app is running on the phone FROM Xcode OR im already in the hotspot when it starts, it runs fine. If i run the app independent of xcode and walk into the hotspot, it crashes or it takes over 60 seconds to switch views. It realizes its supposed to switch, it just doesnt do it for 60+ seconds.

Katushai
  • 1,510
  • 2
  • 14
  • 27
  • I can't see why this should take 45 seconds, but you're doing several things wrong. You shouldn't use sleep at all -- if you want to delay the presentation, use performSelector:withObject:afterDelay:. Also, it's not good practice to add another controller's view to your view without making that controller a child view controller -- a better approach would be to present those controllers modally. – rdelmar Sep 11 '13 at 15:12
  • when you say child, you mean like a UIView inside the view controller? or like a container view placed inside the UIViewController, right? – Katushai Sep 11 '13 at 15:29
  • I mean using the custom container view api, but I think you shouldn't do that, but present the controllers modally instead. So instead of [self.view addSubview: self.transitionView.view];, do [self presentViewController:self.transitionView animated:YES]; – rdelmar Sep 11 '13 at 15:33
  • Thanks for your help, i took your advice and it seems to have improved the time exponentially but its still up to 25+ seconds – Katushai Sep 11 '13 at 15:53
  • You sleep on the main thread?! – Eiko Sep 11 '13 at 15:54
  • 1
    Have you tried running this through the profiler instrument to see what is taking all the time? – Abizern Sep 11 '13 at 15:54
  • Also - instead of sleep() or performSelectorAfterDelay, (so, old school) try using GCD with `dispatch_after()` – Abizern Sep 11 '13 at 15:58
  • Now, is it still slow switching from TransitionViewController to TimerViewController, or just the switch to TransitionViewController? – rdelmar Sep 11 '13 at 15:59
  • slow for both changes; welcome to transition, transition to timer. i knew sleeping on the main thread was bad but im trying every single possible option to see what happens. profiler instrument? never heard of it. got a link? i changed all my sleeps to performSelectorAfterDelay. i can also post code if you guys want. interested in code? i can explain whats happening through the whole thing and you guys can tear me to pieces for how poor the code is :P im just learning objective-c – Katushai Sep 11 '13 at 16:28
  • also i changed all the addSubview:'s to presentViewController:'s. I'm about to test now and see how things go ill let you guys know what happened. – Katushai Sep 11 '13 at 16:29
  • Crash. It just crashed. It didn't take exceptionally long or anything it just closed on me. It doesn't do that when I run it from Xcode, on my iPhone. What's going on?! – Katushai Sep 11 '13 at 16:34

1 Answers1

0

Thanks to everybody that helped me clear up my view controller problems. I'm an experienced programmer, but I have little experience with objective-c/cocoa touch. Anyways, doing what was suggested with the view controllers sped the transition up exponentially. That didn't, however, solve my main problem. The problem was it was either taking a very long time to load the app, or it would crash completely. I figured out that the problem was the fact that the hotspot was right outside my work, so when i went out to test it and walked into the hotspot, the wifi connection was weak and shaky and in the process of switching to LTE. During that time, the app realized it was in a hotspot and was trying to pull a lot of data from the server via NSURL and deserialize it via NSJSONSerialization. So, when i walked into the hotspot, it realized because it has the GPS chip tracking the phone but the wifi was switching over to cellular data and the app was trying its hardest to download the data from the server, to no avail. Anyways, the problem was resolved by turning off wifi while testing for now. Down the road, I intend to build in safeguards designed to protect against that kind of messy event. Thanks again for everybody's help. Have a good day.

Katushai
  • 1,510
  • 2
  • 14
  • 27