How to synchronise callback function when in a loop - iphone

I am pretty new to iPhone development. i need some help on how to synchronize a callback method and a for loop.
For example:
I have a for loop say 1 to 3.
Within this loop, first i send message to a receiver. The result from the receiver is obtained in a callback function. With this result i need to perform some parsing. Now how can i continue with the loop??
BR,
Suppi
Edited with Code:
-(void)requestData{
for (int i=1; i<3; i++) {
completeMessage = [self generateMessage:message];
[self sendMessageToReceiver:completeMessage];
//now it goes to the callback function to read message from receiver. How do i return to this point?? to continue the loop.
[self dosomething:result];
}
}

I don't know much about iPhone development but based on my asynchronous function calling experience you might have to reconsider your approach - assuming this is an asynchronous function call.
When you go through the loop the first time, your code is going to call all the asynchronous functions and move on. It is not going to wait. If you want it to wait for each function call then you either shouldn't use asynchronous functions or use a thread.wait or thread.sleep function in the loop. You could also use some kind of thread synchronization and signalling in the loop. For example, you could make the asynchronous call and then your thread waits until it gets a signal from your callback to continue.
You may want to take your custom end processing out of the loop and do it after all your callbacks are done. You could put state in a common location for each of your callbacks and use it after the callbacks are done.
Of course, you would need to wait until all the callbacks are done before you can continue.
Hope this helps.

Launch the message in a separate thread:
[receiver performSelectorInBackground:#selector(doSomething)];
use performSelectorInBackground:withObject: if you wish to pass a parameter.

Convert your "for" loop into the equivalent goto statements. Then break the goto basic blocks into methods and method calls without goto's. Then break the method containing the wait into 2 methods and use an asynchronous call and callback in between them. You may have to save some of the local and for loop's implicit state in instance variables.
Goto's are not always bad. They are just implicit in more readable structured and/or OOP messaging constructs. Sometimes the compiler can't do the conversion for you, so you need to know enough about raw program control sequencing to do it yourself.

Related

What is use case of thenAcceptAsync in lettuce

What is use case of thenAcceptAsync? There is an explanation on https://lettuce.io/core/release/reference/#asynchronous-api, but I do not get it.
I thought thenAccept is already handling the event async and it is not blocking the main thread. In what use cases do I need thenAcceptAsync? It'd be great if there is an example.
thenAccept is executed in the event loop of netty, if you are calling a long/blocking operation using thenAccept you are going to block the event loop.
So in this case you can run a long running/blocking part of your code in a separate executor to keep event loop free.

How is Callback handling implemented?

In the past, I have used libraries that would allow me to register a callback so that the library can call my method when some event happens (e.g. it is common to see in code that use GUI libraries to look like button.onClick(clickHandler)).
Naively, I suppose the library's handling mechanism could be implemented like:
while(1){
if (event1) { event1Handler(); }
if (event2) { event2Handler(); }
...
}
but that would be really wasteful right? Or is that really how it is done (for instance do well known GUI libraries like java swing, or GTK+ do it this way)?
background:
This question hadn't really occured to me until I encountered curses. I thought about implementing my own callback system, until I realized I didn't know how.
The while loop will typically wait for an interrupt from the user (GetMessage in Windows). When an interrupt arrives GetMessage returns and then it ends up in the callback function. The if statements are typically implemented as a switch-case. See Windows Message Loop on Wikipedia.
In more detail, what happens is the following:
The user application calls GetMessage, which forces the process to sleep until an input message for that application arrives from the systems queue. When a message arrives, the user app calls DispatchMessage, which calls the callback function associated with the window that the message was aimed at.
Windows API uses one callback which handles all events in a switch case. Other libraries use one callback per event class instead.
The function pointers themselves are stored together with other window data in a struct.
Callback system implementation probably has different implementation in different technologies, however, I suppose they should be working this way:
A data structure stores the callback IDs and pointers to the handlers.
A callback handler has a validator
Event handlers have callback callers, which know what are the possible callbacks and check their validity this way:
for each callback in event.callbacks
if (callback.isValid())
call callback()
end if
end for
When you add a handler to a function the system will automatically know where the callback is valid and will add the callback to the datastructure described in 1.
Correct me if I'm wrong, this description is just a guess.

Is there an advantage to using blocks over functions in Objective-C?

I know that a block is a reusable chunk of executable code in Objective-C. Is there a reason I shouldn't put that same chunk of code in a function and just called the function when I need that code to run?
It depends on what you're trying to accomplish. One of the cool things about blocks is that they capture local scope. You can achieve the same end result with a function, but you end up having to do something like pass around a context object full of relevant values. With a block, you can do this:
int num1 = 42;
void (^myBlock)(void) = ^{
NSLog(#"num1 is %d", num1);
};
num1 = 0; // Changed after block is created
// Sometime later, in a different scope
myBlock(); // num1 is 42
So simply by using the variable num1, its value at the time myBlock was defined is captured.
From Apple's documentation:
Blocks are a useful alternative to traditional callback functions for
two main reasons:
They allow you to write code at the point of invocation that is
executed later in the context of the method implementation. Blocks are
thus often parameters of framework methods.
They allow access to local variables. Rather than using callbacks
requiring a data structure that embodies all the contextual
information you need to perform an operation, you simply access local
variables directly.
As Brad Larson comments in response to this answer:
Blocks will let you define actions that take place in response to an
event, but rather than have you write a separate method or function,
they allow you to write the handling code right where you set up the
listener for that event. This can save a mess of code and make your
application much more organized.
A good example of which i can give you is of alert view, it will be good if i decided at time of creation of alert view what will happen when i dismiss that instead i write the delegate method and wait for that to call. So it will be much easier to understand and implement and also it provides fast processing.

What is the difference between GCD Dispatch Sources and select()?

I've been writing some code that replaces some existing:
while(runEventLoop){
if(select(openSockets, readFDS, writeFDS, errFDS, timeout) > 0){
// check file descriptors for activity and dispatch events based on same
}
}
socket reading code. I'd like to change this to use a GCD queue, so that I can pop events on to the queue using dispatch_async instead of maintaining a "must be called on next iteration" array. I also am already using a GCD queue to /contain/ this particular action, hence wanting to devolve it to a more natural GCD dispatch form. ( not a while() loop monopolizing a serial queue )
However, when I tried to refactor this into a form that relied on dispatch sources fired from event handlers tied to DISPATCH_SOURCE_TYPE_READ and DISPATCH_SOURCE_TYPE_WRITE on the socket descriptors, the library code that depended on this scheduling stopped working. My first assumption is that I'm misunderstanding the use of DISPATCH_SOURCE_TYPE_READ and DISPATCH_SOURCE_TYPE_WRITE - I had assumed that they would yield roughly the same behavior as calling select() with those socket descriptors.
Do I misunderstand GCD dispatch sources? Or, regarding the refactor, am I using it in a situation where it is not best suited?
The short answer to your question is: none. There are no differences, both GCD dispatch sources and select() do the same thing: they notify the user that a specific kernel event happened or that a particular condition holds true.
Note that, on a mac or iOS device you should not use select(), but rather the more advanced kqueue() and kevent() (or kevent64()).
You may certainly convert the code to use GCD dispatch sources, but you need to be careful not to break other code relying on this. So, this needs a complete inspection of the whole code handling signals, file descriptors, socket and all of the other low level kernel events.
May be a simpler solution could be to maintain the original code, simply adding GCD code in the part that react to events. Here, you dispatch events on different queues depending on the particular type of event.

What is a callback?

Is it a function?
Is it a function being called from the source?
Or, is it a function being returned from the destination?
Or, is it just executing a function at the destination?
Or, is it a value returned from a function passed to the destination?
A callback is the building block of asynchronous processing.
Think of it this way: when you call someone and they don't answer, you leave a message and your phone number. Later on, the person calls you back based on the phone number you left.
A callback works in a similar manner.
You ask an API for a long running operation and you provide a method from within your code to be called with the result of the operation. The API does its work and when the result is ready, it calls your callback method.
From the great Wikipedia:
In computer programming, a callback is
executable code that is passed as an
argument to other code. It allows a
lower-level software layer to call a
subroutine (or function) defined in a
higher-level layer.
Said another way, when you pass a callback to your method, it's as if you are providing additional instructions (e.g., what you should do next). An attempt at making a simple human example follows:
Paint this wall this shade of green (where "paint" is analagous to the method called, while "wall" and "green" are similar to arguments).
When you have finished painting, call me at this number to let me know that you're done and I'll tell you what to do next.
In terms of practical applications, one place where you will sometimes see callbacks is in situations with asynchronous message passing. You might want to register a particular message as an item of interest for class B.
However, without something like a callback, there's no obvious way for class A to know that class B has received the message. With a callback, you can tell class B, here's the message that I want you to listen for and this is the method in class A that I want you to call when you receive it.
Here is a Java example of a callback from a related question.