トピック購読の有無の確認
モバイルアプリのプログラムから、特定のトピックが存在するかどうかを確認することができます。
以下に、アプリケーションスコープにトピックが存在するかどうかを確認する例を挙げます(グループスコープのトピックとユーザースコープのトピックについても、基本的な操作は同様です)。
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."); } }];
ここでは次の処理を行っています。
- 存在を確認したいトピックのインスタンスを取得。
checkIfExistsSynchronous:
メソッドを実行し、トピックの存在有無を確認。