I want to write an App, that can run two different “threads”, one for a Server one for a Client. I have heard, that in Objective-C Dispatch Queues and Blocks are the way to go. I have read the Apple Documentation about Blocks and Dispatch Queues to quite a large extend, however I still fail at the very beginning of my coding.
Am I right, that I need to create 2 Blocks, one for my server code and one for my client code and then put those blocks in the dispatch queue to run concurrently?
I was now trying to create a block for my server, and failed miserably. I tried the following:
#import <Foundation/Foundation.h>
@interface Server : NSObject {
void (^server)(NSString*, int);
}
@end
And then
#import "Server.h"
@implementation Server
server = ^(NSString* host, int port) {
};
@end
I thought I would have to declare the block variable first in a header file, so that I can later refer to it in my ViewController. Any answers about the general structure with some code examples would be highly appreciated 🙂
I would advise creating a
typedef, so you can easily create blocks of that type. Like this:On your .h:
On your .m:
You can put your
typedefinside a .h file called constants and then import it on your .pch file (this is just suggestion…)