ICL1901
ICL1901

Reputation: 7778

iOS - wait till a process ends

This is an IOS6 question.

I have an app that is calling a class (A) to check something. Then I want to call a class (B) to do something else Is it possible to make sure process B doesn't start before process A finishes?

At the moment, I just call one after the other in the RootVC.

Each is showing a modal view, and I only get to see B ..

[self performA];
[self performB];

Thanks

Upvotes: 2

Views: 663

Answers (2)

AngryLemming
AngryLemming

Reputation: 1

I took the dare and failed. The story: My app has been giving me hell updating from an iOS4 target to iOS6 (with a contingent sub of code for iOS5/3GS). It crashes unless i use @try etc... with a built in delay interval on the reattempt (which is stupid, 'cause I don't know how large a database the users have, nor how long it will take to load them). It's a painful way to get around my real problem: the view loads before the CoreData stack (logs) can be loaded completely and I don't see a way to make the initial view wait until its NSMutableArray (based on the CoreData database of my object) loads. Basically, I keep getting a false error about addObjectsSortedBy: the foremost attribute of my entity. Threading does seem to be the answer, but I need to load an NSMutableArray and feed it into my initialViewController, which will be visible on every launch (excluding FirstTime initial), but my attempt (okay, 12 attempts) to use threading just made the crash occur earlier in the app launch. The result: I bow down to those who have wrangled that bull of threads. My solution has been to build in a notification in the AppDelegate.m, my initialViewController viewDidLoad is told to listen for it before anything else. If it gets the notification it skips ahead and completes the normal process unto [super viewDidLoad]; if not, it executes @try, @catch, @finally. In the @try I attempt to proceed as though the notification arrived (like it was a little late), then I handle (@catch) the error by displaying a "Please Wait" label to the user, then I tell the app to wait .xx and repeat the original addObjectsSortedBy: command as though everything were kösher to begin with.The sweet-spot for my app, with images and data in the logs appears to be .15 for the wait interval @50 test entries, with time to spare and no obvious lag on load. I could probably go down to .10 @50 entries. BUT: I don't know how to scale this, without having the logs loaded enough to get an object.count! Without that, there is no way to scale my delay, which means it may (read:will) not work for large logs with many entries (200+)! I have a work-around, but I'm going to keep trying to get a grip on threading, in order to have a solution. And to be honest, once I hit 20 entries, the notification never hits in time for the @try to occur. If you can, use threads. I painted myself into a corner by failing to do so early on and am paying for it: my app has been in need of an overhaul, but I need this notch in my belt before it will be worthwhile. The earlier you can implement threaded loading the better for your long-term development. In the meantime, you may be able to use my work-around to continue testing other parts of your app.

Upvotes: 0

Jonah
Jonah

Reputation: 17958

There are several tools for managing the order of execution of parts of your application available to you. However since you are presenting view controllers you have a couple of constraints; you don't want to block the main thread (or else the app will become unresponsive) and you must perform UI actions on the main thread.

In this case the most common, and probably most appropriate, solution is to setup a callback to trigger action B when action A finishes.

The modal view controller presented as part of A might call a delegate when it has finished its task successfully. That delegate can then begin task B.

Alternately you might pass a block to A which A will execute when it finishes. That block can then perform task B.

Upvotes: 2

Related Questions