Skip to content

Commit 5afe2be

Browse files
committed
add comments
1 parent c3bbb2a commit 5afe2be

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

MQTTExample/MQTTViewController.m

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
@interface MQTTViewController ()
1616

17+
// this UISwitch will be used to display the status received from the topic.
1718
@property (weak, nonatomic) IBOutlet UISwitch *subscribedSwitch;
1819

20+
// create a property for the MQTTClient that is used to send and receive the message
1921
@property (nonatomic, strong) MQTTClient *client;
2022

2123
@end
@@ -26,54 +28,62 @@ - (void)viewDidLoad
2628
{
2729
[super viewDidLoad];
2830

31+
// create the MQTT client with an unique identifier
2932
NSString *clientID = [UIDevice currentDevice].identifierForVendor.UUIDString;
3033
self.client = [[MQTTClient alloc] initWithClientId:clientID];
3134

35+
// keep a reference on the switch to avoid having a reference to self in the
36+
// block below (retain/release cycle, blah blah blah)
3237
UISwitch *subSwitch = self.subscribedSwitch;
3338

34-
// define the message handler that will handle the received MQTT messages
39+
// define the handler that will be called when MQTT messages are receive by the client
3540
[self.client setMessageHandler:^(MQTTMessage *message) {
41+
// extract the switch status from the message payload
3642
BOOL on = [message.payloadString boolValue];
43+
3744
// the MQTTClientDelegate methods are called from a GCD queue.
3845
// Any update to the UI must be done on the main queue
3946
dispatch_async(dispatch_get_main_queue(), ^{
4047
[subSwitch setOn:on animated:YES];
4148
});
4249
}];
50+
4351
// connect the MQTT client
4452
[self.client connectToHost:kMQTTServerHost completionHandler:^(NSUInteger code) {
53+
// The client is connected when this completion handler is called
4554
NSLog(@"client is connected with id %@", clientID);
46-
// once the client is connected, subscribe to the topic
47-
[self.client subscribe:kTopic
48-
withCompletionHandler:^(NSArray *grantedQos) {
55+
// Subscribe to the topic
56+
[self.client subscribe:kTopic withCompletionHandler:^(NSArray *grantedQos) {
57+
// The client is effectively subscribed to the topic when this completion handler is called
4958
NSLog(@"subscribed to topic %@", kTopic);
5059
}];
5160
}];
5261
}
5362

5463
- (void)dealloc
5564
{
65+
// disconnect the MQTT client
5666
[self.client disconnectWithCompletionHandler:^(NSUInteger code) {
67+
// The client is disconnected when this completion handler is called
5768
NSLog(@"MQTT is disconnected");
5869
}];
5970
}
60-
- (void)didReceiveMemoryWarning
61-
{
62-
[super didReceiveMemoryWarning];
63-
// Dispose of any resources that can be recreated.
64-
}
6571

6672
#pragma mark - IBActions
6773

74+
// This method is called when the "published LED" switch status changes
6875
- (IBAction)switchUpdated:(id)sender {
6976
BOOL on = [sender isOn];
7077
NSString *payload = [NSNumber numberWithBool:on].stringValue;
7178

79+
// use the MQTT client to send a message with the switch status to the topic
7280
[self.client publishString:payload
7381
toTopic:kTopic
7482
withQos:AtMostOnce
7583
retain:YES
7684
completionHandler:nil];
85+
// we passed nil to the completionHandler as we are not interested to know
86+
// when the message was effectively sent
7787
}
7888

7989
@end

0 commit comments

Comments
 (0)