Rose Perrone
Rose Perrone

Reputation: 63586

Order of execution of async callback methods one one thread

Let's say a network message kicks off a callback method.

- (void)didReceiveNetworkMessage {
  [obj respondToMethod];
}

- (void)myBigMethod {
  [obj mySmallMethod1];
  [obj mySmallMethod2];
  /* network message received now */
  [obj mySmallMethod3];
  [obj mySmallMethod4];
}

In what order are the methods called on obj executed in a single-threaded environment?

Upvotes: 1

Views: 117

Answers (4)

TheCodingArt
TheCodingArt

Reputation: 3429

It depends on how everything is pushed into the stack heap. You're best bet would be to watch you're stack (print out messages) and such to figure that out. The heap defines the order of execution. If there is a problem with delay, manipulating the heap order will probably fix it for you. If you need something to execute at the same time, multithreading and tossing in block code should help.

- (void)didReceiveNetworkMessage {
  [obj respondToMethod];
}

- (void)myBigMethod {
  [obj mySmallMethod1];
  [obj mySmallMethod2];
  /* network message received now */
  [obj mySmallMethod3];
  [obj mySmallMethod4];
}

From the code above with the noted reference it seems like respondToMethod, mySmallMethod1,2,3,4 are called in that order.

I'm guessing I have to clarify that with the complete lack of information provided above (including what messages are actually calling these methods and where). Without that information my answer stands as correct. No clue why I got voted down for that. I've stated the order that the methods are called and executed with the assumption that you receive a call back from respondToMethod before mySmallMethod3 and after mySmallMethod2. This is what has been stated in the above example. If you wish for a better answer, provide a better more clarified example.

Upvotes: -1

jrturton
jrturton

Reputation: 119272

Assuming everything above is on the main thread, myBigMethod will finish executing (so mySmallMethod4 will be called) before the didReceiveNetworkMessage method is processed.

The callback is typically performed on the main thread, since it is done after background work (i.e. network loading) is complete. Unless you've specifically coded it otherwise, everything generally happens on the main thread.

Upvotes: 2

Catfish_Man
Catfish_Man

Reputation: 41821

Methods that aren't on the same thread, same queue, or a queue that targets the same queue have undefined ordering relative to each other. Individual instructions of each could occur at any time during the other, including simultaneously.

If the methods are being run in a serialized context like that, then the ordering is dependent on which one started first; the same concerns apply to starting the method though, so that order is also undefined.

Upvotes: 0

rmaddy
rmaddy

Reputation: 318874

Update: Given the updated question, then if everything is done on the same thread then respondToMethod will be called after myBigMethod is done.

If multiple threads were involved then assuming the didReceiveNetworkMessage is called on a different thread than the myBigMethod method, you will see:

mySmallMethod1
mySmallMethod2
mySmallMethod3
mySmallMethod4

and respondToMethod can be called at any time while mySmallMethod2 is begin executed or later.

In other words, respondToMethod may run at the same time as mySmallMethod2, mySmallMethod3, or mySmallMethod4 or even after mySmallMethod4 is done.

Upvotes: 1

Related Questions