Skip to content

Subscribe Event

The data collector provides API to let an event consumer subscribe to a certain list of events. A callback function must be provided so the DataCollector can notify and call the specified callback. The callback function should be non-blocking and return quickly.

Subscribe Event

Subscribe Method Parameters Details
consumerWithName name: String Set the consumer name as an identifier for DataCollector
forEventTypes types: [TNEventType] Set the list of event types to subscribe to
withCallBack block: @escaping (_ occurredEvent: TNEvent)->() Set the callback function which should be implemented

This method returns an error (TNDataCollectorResponse) in case of consumer name is empty (.emptyConsumerName), consumer with given name was already added for given types (.existedConsumerForType) or list of event types is empty (.emptyTypes). It returns .success in case of successful adding of subscriber.

1
2
3
4
5
6
let types = TNEventType.Favorite.values
dataCollectorClient.subscribe(consumerWithName: "consumer_1",
                              forEventTypes: types,
                              withCallBack: { (event) in
                                 print("An event happened: \(event.type.label)")
                              })
1
2
3
4
5
6
NSArray * types = [TNEventType.Favorite values];
[dataCollectorClient subscribeWithConsumerWithName:@"consumer_1"
                                     forEventTypes:types
                                      withCallBack:^(TNEvent * _Nonnull event) {
    NSLog(@"An event happened");
}];