user1114881
user1114881

Reputation: 751

How do I stop MQTTClient from getting new data

I have an app which uses MQTTClient to connect to io.adafruit and then subscribes to various feeds. I then publish a "GET" message to start receiving data from the various feeds. Then I check the various feeds to see what they are and do whatever. This is fine when the app is in the foreground but when it is put into the background I would like to stop the process. I have tried setting the session to nil, disconnect the session, and set the MQTTCRSocketTransport to nil all to no avail. It just keeps checking for updates. This is my code.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.myUserName =  [defaults objectForKey:@"myUserNameFile"];
    NSLog(@"myUserName:%@",self.myUserName);
    self.myUserKey =  [defaults objectForKey:@"myUserKeyFile"];
    NSLog(@"myUserKey:%@",self.myUserKey);
    
    transport = [[MQTTCFSocketTransport alloc] init];
    transport.host = @"io.adafruit.com";
    transport.port = 1883;
    
    NSLog(@"transport:%@",transport);
    session2 = [[MQTTSession alloc] init];
    session2.userName =self.myUserName;
    session2.password = self.myUserKey;
    session2.transport = transport;
    
    session2.delegate = self;
    session2.keepAliveInterval = 30;
    NSString *feeds = [self.myUserName stringByAppendingString:@"/feeds/"];
    
    self.battery = [feeds stringByAppendingString:@"battery"];
    
    self.getbat = [self.battery stringByAppendingString:@"/get"];
    
     [session2 connectWithConnectHandler:^(NSError *error) {
            if(!error){
                
          [session2 subscribeToTopic:self.battery atLevel:1 subscribeHandler:^(NSError *error, NSArray *gQoss){
                    
            if (error) {
                NSLog(@"Subscription failed %@", error.localizedDescription);
            } else {
                NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
                
               
                NSData* data = [ @"0" dataUsingEncoding:NSUTF8StringEncoding];
                [session2 publishData:data onTopic:self.getbat retain:NO qos:0 publishHandler:nil];
                 }
        }]; }
            else {NSLog(@"[connectWithConnectHandler]Error Connect %@", error.localizedDescription);}
      }];
 }

- (void)newMessage:(MQTTSession *)session
              data:(NSData *)data
           onTopic:(NSString *)topic
               qos:(MQTTQosLevel)qos
          retained:(BOOL)retained
               mid:(unsigned int)mid {
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

 if ([topic isEqualToString:self.battery]){
       B = [dataString intValue];
       }

- (void)applicationDidEnterBackground:(UIApplication *)application
{  [session2 disconnect];
   [session2 unsubscribeTopic:self.battery];
   NSLog(@"this enters background");}

Can someone help me stop the process when the app enters the background. The biggest problem is that one of the feeds updates it data every 6seconds so when the app enters the background these updates keeps the app alive in the background.

Upvotes: 0

Views: 38

Answers (0)

Related Questions