1

I want to implement a library for the America's Cup Data Stream API in Objective-C and was looking around if there already existed one and found an implementation in C# which I find beautiful and want to borrow some ideas from. This being the first C# source code I've seen so I do not fully understand what is happening.

The following is a sample program that using the C# library. It starts a Client that deal with network communication and a FeedEvents that takes care of the message dispatch. Then something interesting happens, it looks like a lambda expression is being used to specify the action when OnChatterText occurs, right? How do I do this in Objective-C? Using blocks?

class Program
{
    static void Main(string[] args)
    {
        var c = new Client();
        var e = new FeedEvents();
        e.OnChatterText += ch => Console.WriteLine(string.Format("{0}: {1}",
            ch.Source, ch.Text));

        c.OnMessage += e.MessageHandler;
        c.Connect();

        Thread.Sleep(Timeout.Infinite);
    }
}
norq
  • 1,404
  • 2
  • 18
  • 35

1 Answers1

1

Here is the similar to method you specified in your question, written in objc: Supposably we have a class Client and FeedEvents. Client class is implemented to handle network connectivity (like using AFNetworking for example). FeedEvents is interconnected with Client class to handle events coming from network, but this events has to call "client" block of code to make things simpler.

@interface Client : NSObject

- (void)connect;

@end

typedef void (^EventBlock) (id someData);

@interface FeedEvents : NSObject

@property (nonatomic, strong) EventBlock eventBlock;

@end

@interface ProgramClass : NSObject

- (void)fetchEvents;

@end

@implementation ProgramClass

- (void)fetchEvents {
    // I prefer using singleton for generic instances that are used through entire application
    // in the same way, but also sometimes it's better to use this approach:
    // ...

    Client *connectionClient = [Client new]; // or custom -[+[ alloc] initWithParameters...];
    FeedEvents *eventListener = [FeedEvents new]; // or custom -[+[ alloc] initWithParameters...];
    eventListener.eventBlock = ^(id someData) {
        // do some additional configuration
    };

    // using background queue to connect to some host
    // but this is only for purpose of the method translation, because
    // AFNetworking provides very reach background API
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, kNilOptions), ^{

        [connectionClient connect];
    };
}

@end

You can also subclass NSOperation to use this - (void)main {} method as in your example and run it completely in background thread.

In this example code you should know that there are some memory management issues with block parameters etc. So simpler approach for you in this case is to subclass NSOperation and run it in background like here.

Hope this is helpful

Community
  • 1
  • 1
art-divin
  • 1,635
  • 1
  • 20
  • 28