Checking if a Topic Exists

You can check if a certain topic already exists from your mobile application.

The following example checks if the specified topic already exist in an application scope (you can similarly check the topic existence in a user and group scopes).

Swift:

  • // Instantiate the target topic.
    let topic = Kii.topic(withName: "SendingAlert")
    
    do {
      // Check if the topic exists.
      try topic.checkIfExistsSynchronous()
    } catch {
      print("The topic does not exist.")
      return
    }
    
    print("The topic already exists.")
  • // Instantiate the target topic.
    let topic = Kii.topic(withName: "SendingAlert")
    
    // Check if the topic exists.
    topic.checkIfExists { (topic : KiiSubscribable , isExists : Bool, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
      if (isExists) {
        print("The topic already exists.")
      } else {
        print("The topic does not exist.")
      }
    }

Objective-C:

  • NSError *error = nil;
    
    // Instantiate the target topic.
    KiiTopic *topic = [Kii topicWithName:@"SendingAlert"];
    
    // Check if the topic exists.
    BOOL isExists = [topic checkIfExistsSynchronous:&error];
    
    if (error != nil){
      // Handle the error.
      return;
    }
    
    if (isExists) {
      NSLog(@"The topic already exists.");
    } else {
      NSLog(@"The topic does not exist.");
    }
  • // Instantiate the target topic.
    KiiTopic *topic = [Kii topicWithName:@"SendingAlert"];
    
    // Check if the topic exists.
    [topic checkIfExists:^(KiiTopic *aTopic, BOOL isExists, NSError *error) {
      if (error != nil){
        // Handle the error.
        return;
      }
    
      if (isExists) {
        NSLog(@"The topic already exists.");
      } else {
        NSLog(@"The topic does not exist.");
      }
    }];

Here is a description of the sample code:

  • Instantiates a topic.
  • Calls the checkIfExistsSynchronous: method to see if the topic is exist or not.