When a thing is registered on Kii Cloud, a dedicated thing scope is created for it. You can leverage the Kii push notification features with this scope's bucket and topics (See Thing scope for the overview of the thing scope).
The way you leverage the push notification with a thing scope's bucket and topic is the same as that of other scopes. See Push to App Notification and Push to User Notification to learn more.
Using the Push to App notification feature
In the following examples, a new bucket is created in a thing scope and a user is subscribing to this bucket.
Android
try {
// Instantiate a thing by vendor thing ID.
KiiThing thing = KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" );
// Create a bucket.
KiiBucket thingBucket = thing . bucket ( "thing_bucket" );
// Create and save a KiiObject in the bucket.
thingBucket . object (). save ();
// Subscribe to the bucket.
KiiUser . getCurrentUser (). pushSubscription (). subscribe ( thingBucket );
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , new KiiCallback < KiiThing >() {
@Override
public void onComplete ( KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Create a bucket.
KiiBucket thingBucket = result . bucket ( "thing_bucket" );
// Create and save a KiiObject in the bucket.
thingBucket . object (). save ( new KiiObjectCallBack () {
@Override
public void onSaveCompleted ( int token , KiiObject object , Exception exception ) {
if ( exception != null ) {
// Handle the error.
return ;
}
// Subscribe to the bucket.
KiiUser . getCurrentUser (). pushSubscription (). subscribe ( thingBucket , new KiiPushCallBack () {
@Override
public void onSubscribeCompleted ( int taskId , KiiBucket target , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
}
});
}
});
}
});
iOS
Swift:
let thing : KiiThing
do {
// Instantiate a thing by vendor thing ID.
thing = try KiiThing . loadSynchronous ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" )
} catch ( let error as NSError ){
// Handle the error.
return
}
// Create a bucket.
let thingBucket = thing . bucket ( withName : "thing_bucket" )
// Create a KiiObject in the bucket.
let object = thingBucket . createObject ()
do {
// Save the KiiObject.
try object . saveSynchronous ()
// Subscribe to the bucket.
try KiiUser . current () !. pushSubscription () . subscribeSynchronous ( thingBucket )
} catch let error as NSError {
// Handle the error.
return
}
// Instantiate a thing by vendor thing ID.
KiiThing . load ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" ) { ( thing , error ) -> Void in
if error != nil {
// Handle the error.
return
}
// Create a bucket.
let thingBucket = thing !. bucket ( withName : "thing_bucket" )
// Create and save a KiiObject in the bucket.
let object = thingBucket . createObject ()
object . save { ( retObject : KiiObject ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Subscribe to the bucket.
KiiUser . current () !. pushSubscription () . subscribe ( thingBucket , block : { ( subscription : KiiPushSubscription ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
})
}
}
Objective-C:
NSError * error = nil ;
// Instantiate a thing by vendor thing ID.
KiiThing * thing = [ KiiThing loadSynchronousWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a bucket.
KiiBucket * thingBucket = [ thing bucketWithName : @"thing_bucket" ];
// Create and save a KiiObject in the bucket.
KiiObject * object = [ thingBucket createObject ];
[ object saveSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the bucket.
[[ KiiUser currentUser ]. pushSubscription subscribeSynchronous : thingBucket
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Instantiate a thing by vendor thing ID.
[ KiiThing loadWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a bucket.
KiiBucket * thingBucket = [ thing bucketWithName : @"thing_bucket" ];
// Create and save a KiiObject in the bucket.
KiiObject * object = [ thingBucket createObject ];
[ object saveWithBlock : ^ ( KiiObject * object , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the bucket.
[[ KiiUser currentUser ]. pushSubscription subscribe : thingBucket
block : ^ ( KiiPushSubscription * subscription , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
}];
}];
JavaScript
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , {
success : function ( thing ) {
// Create a bucket.
var thingBucket = thing . bucketWithName ( "thing_bucket" );
// Create a KiiObject in the bucket.
var object = thingBucket . createObject ();
// Save the KiiObject.
object . save ({
success : function ( theObject ) {
// Subscribe to the bucket.
KiiUser . getCurrentUser (). pushSubscription (). subscribe ( thingBucket , {
success : function ( thePushSubscription , theBucket ) {
// Do something.
},
failure : function ( errorString ) {
// Handle the error.
}
});
},
failure : function ( theObject , errorString ) {
// Handle the error.
}
});
},
failure : function ( errorString ) {
// Handle the error.
}
});
In the next examples, a new bucket is created in a thing scope and this time we are making the thing itself to subscribe to this bucket.
Android
try {
// Instantiate a thing by vendor thing ID.
KiiThing thing = KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" );
// Create a bucket.
KiiBucket thingBucket = thing . bucket ( "thing_bucket" );
// Create and save a KiiObject in the bucket.
thingBucket . object (). save ();
// Subscribe to the bucket.
thing . pushSubscription (). subscribe ( thingBucket );
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , new KiiCallback < KiiThing >() {
@Override
public void onComplete ( KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Create a bucket.
KiiBucket thingBucket = result . bucket ( "thing_bucket" );
// Create and save a KiiObject in the bucket.
thingBucket . object (). save ( new KiiObjectCallBack () {
@Override
public void onSaveCompleted ( int token , KiiObject object , Exception exception ) {
if ( exception != null ) {
// Handle the error.
return ;
}
// Subscribe to the bucket.
result . pushSubscription (). subscribe ( thingBucket , new KiiPushCallBack () {
@Override
public void onSubscribeCompleted ( int taskId , KiiBucket target , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
}
});
}
});
}
});
iOS
Swift:
let thing : KiiThing
do {
// Instantiate a thing by vendor thing ID.
thing = try KiiThing . loadSynchronous ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" )
} catch ( let error as NSError ){
// Handle the error.
return
}
// Create a bucket.
let thingBucket = thing . bucket ( withName : "thing_bucket" )
// Create a KiiObject in the bucket.
let object = thingBucket . createObject ()
do {
// Save the KiiObject.
try object . saveSynchronous ()
// Subscribe to the bucket.
try thing . pushSubscription () . subscribeSynchronous ( thingBucket )
} catch let error as NSError {
// Handle the error.
return
}
// Instantiate a thing by vendor thing ID.
KiiThing . load ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" ) { ( thing , error ) -> Void in
if error != nil {
// Handle the error.
return
}
// Create a bucket.
let thingBucket = thing !. bucket ( withName : "thing_bucket" )
// Create and save a KiiObject in the bucket.
let object = thingBucket . createObject ()
object . save { ( retObject : KiiObject ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Subscribe to the bucket.
thing !. pushSubscription () . subscribe ( thingBucket , block : { ( subscription : KiiPushSubscription ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
})
}
}
Objective-C:
NSError * error = nil ;
// Instantiate a thing by vendor thing ID.
KiiThing * thing = [ KiiThing loadSynchronousWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
error : & error ];
// Create a bucket.
KiiBucket * thingBucket = [ thing bucketWithName : @"thing_bucket" ];
// Create and save a KiiObject in the bucket.
KiiObject * object = [ thingBucket createObject ];
[ object saveSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the bucket.
[ thing . pushSubscription subscribeSynchronous : thingBucket
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Instantiate a thing by vendor thing ID.
[ KiiThing loadWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a bucket.
KiiBucket * thingBucket = [ thing bucketWithName : @"thing_bucket" ];
// Create and save a KiiObject in the bucket.
KiiObject * object = [ thingBucket createObject ];
[ object saveWithBlock : ^ ( KiiObject * object , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the bucket.
[ thing . pushSubscription subscribe : thingBucket
block : ^ ( KiiPushSubscription * subscription , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
}];
}];
JavaScript
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , {
success : function ( thing ) {
// Create a bucket.
var thingBucket = thing . bucketWithName ( "thing_bucket" );
// Create a KiiObject in the bucket.
var object = thingBucket . createObject ();
// Save the KiiObject.
object . save ({
success : function ( theObject ) {
// Subscribe to the bucket.
thing . pushSubscription (). subscribe ( thingBucket , {
success : function ( thePushSubscription , theBucket ) {
// Do something.
},
failure : function ( errorString ) {
// Handle the error.
}
});
},
failure : function ( theObject , errorString ) {
// Handle the error.
}
});
},
failure : function ( errorString ) {
// Handle the error.
}
});
Using the Push to User notification feature
In the following examples, a new topic is created in a thing scope and a user is subscribing to this topic.
Android
try {
// Instantiate a thing by vendor thing ID.
KiiThing thing = KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" );
// Create a topic.
KiiTopic thingTopic = thing . topic ( "thing_topic" );
// Save the topic.
thingTopic . save ();
// Subscribe to the topic.
KiiUser . getCurrentUser (). pushSubscription (). subscribe ( thingTopic );
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , new KiiCallback < KiiThing >() {
@Override
public void onComplete ( KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Create a topic.
KiiTopic thingTopic = result . topic ( "thing_topic" );
// Save the topic.
thingTopic . save ( new KiiTopicCallBack () {
@Override
public void onSaveCompleted ( int taskId , KiiTopic target , Exception exception ) {
if ( exception != null ) {
// Handle the error.
return ;
}
// Subscribe to the topic.
KiiUser . getCurrentUser (). pushSubscription (). subscribe ( target , new KiiPushCallBack () {
@Override
public void onSubscribeCompleted ( int taskId , KiiBucket target , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
}
});
}
});
}
});
iOS
Swift:
let thing : KiiThing
do {
// Instantiate a thing by vendor thing ID.
thing = try KiiThing . loadSynchronous ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" )
} catch ( let error as NSError ){
// Handle the error.
return
}
// Create a topic.
let thingTopic = thing . topic ( withName : "thing_topic" )
do {
// Save the topic.
try thingTopic . saveSynchronous ()
} catch let error as NSError {
// Handle the error.
return
}
do {
// Subscribe to the topic.
try KiiUser . current () !. pushSubscription () . subscribeSynchronous ( thingTopic )
} catch let error as NSError {
// Handle the error.
return
}
// Instantiate a thing by vendor thing ID.
KiiThing . load ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" ) { ( thing , error ) -> Void in
if error != nil {
// Handle the error.
return
}
// Create a topic.
let thingTopic = thing !. topic ( withName : "thing_topic" )
// Save the topic.
thingTopic . save ({ ( thingTopic , error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Subscribe to the topic.
KiiUser . current () !. pushSubscription () . subscribe ( thingTopic , block : { ( subscription : KiiPushSubscription ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
})
})
}
Objective-C:
NSError * error = nil ;
// Instantiate a thing by vendor thing ID.
KiiThing * thing = [ KiiThing loadSynchronousWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a topic.
KiiTopic * thingTopic = [ thing topicWithName : @"thing_topic" ];
// Save the topic.
[ thingTopic saveSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the topic.
[[ KiiUser currentUser ]. pushSubscription subscribeSynchronous : thingTopic
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Instantiate a thing by vendor thing ID.
[ KiiThing loadWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a topic.
KiiTopic * thingTopic = [ thing topicWithName : @"thing_topic" ];
// Save the topic.
[ thingTopic saveWithBlock : ^ ( KiiTopic * topic , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the topic.
[[ KiiUser currentUser ]. pushSubscription subscribe : topic
block :
^ ( KiiPushSubscription * subscription , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
}];
}];
JavaScript
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , {
success : function ( thing ) {
// Create a topic.
thingTopic = thing . topicWithName ( "thing_topic" );
// Save the topic.
thingTopic . save ({
success : function ( theTopic ) {
// Subscribe to the topic.
KiiUser . getCurrentUser (). pushSubscription (). subscribe ( thingTopic , {
success : function ( pushSubscription ) {
// Do something.
},
failure : function ( pushSubscription , error ) {
// Handle the error.
}
});
},
failure : function ( theTopic , error ) {
// Handle the error.
}
});
},
failure : function ( error ) {
// Handle the error.
}
});
In the next examples, a new topic is created in a thing scope and this time we are making the thing itself to subscribe to this topic.
Android
try {
// Instantiate a thing by vendor thing ID.
KiiThing thing = KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" );
// Create a topic.
KiiTopic thingTopic = thing . topic ( "thing_topic" );
// Save the topic.
thingTopic . save ();
// Subscribe to the topic.
thing . pushSubscription (). subscribe ( thingTopic );
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , new KiiCallback < KiiThing >() {
@Override
public void onComplete ( final KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Create a topic.
KiiTopic thingTopic = result . topic ( "thing_topic" );
// Save the topic.
thingTopic . save ( new KiiTopicCallBack () {
@Override
public void onSaveCompleted ( int taskId , KiiTopic target , Exception exception ) {
if ( exception != null ) {
// Handle the error.
return ;
}
// Subscribe to the topic.
result . pushSubscription (). subscribe ( target , new KiiPushCallBack () {
@Override
public void onSubscribeCompleted ( int taskId , KiiBucket target , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
}
});
}
});
}
});
iOS
Swift:
let thing : KiiThing
do {
// Instantiate a thing by vendor thing ID.
thing = try KiiThing . loadSynchronous ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" )
} catch ( let error as NSError ){
// Handle the error.
return
}
// Create a topic.
let thingTopic = thing . topic ( withName : "thing_topic" )
do {
// Save the topic.
try thingTopic . saveSynchronous ()
} catch let error as NSError {
// Handle the error.
return
}
do {
// Subscribe to the topic.
try thing . pushSubscription () . subscribeSynchronous ( thingTopic )
} catch let error as NSError {
// Handle the error.
return
}
// Instantiate a thing by vendor thing ID.
KiiThing . load ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" ) { ( thing , error ) -> Void in
if error != nil {
// Handle the error.
return
}
// Create a topic.
let thingTopic = thing !. topic ( withName : "thing_topic" )
// Save the topic.
thingTopic . save ({ ( thingTopic , error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Subscribe to the topic.
thing !. pushSubscription () . subscribe ( thingTopic , block : { ( subscription : KiiPushSubscription ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
})
})
}
Objective-C:
NSError * error = nil ;
// Instantiate a thing by vendor thing ID.
KiiThing * thing = [ KiiThing loadSynchronousWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a topic.
KiiTopic * thingTopic = [ thing topicWithName : @"thing_topic" ];
// Save the topic.
[ thingTopic saveSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the topic.
[ thing . pushSubscription subscribeSynchronous : thingTopic
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Instantiate a thing by vendor thing ID.
[ KiiThing loadWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Create a topic.
KiiTopic * thingTopic = [ thing topicWithName : @"thing_topic" ];
// Save the topic.
[ thingTopic saveWithBlock : ^ ( KiiTopic * topic , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
// Subscribe to the topic.
[ thing . pushSubscription subscribe : topic
block : ^ ( KiiPushSubscription * subscription , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
}];
}];
JavaScript
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , {
success : function ( thing ) {
// Create a topic.
thingTopic = thing . topicWithName ( "thing_topic" );
// Save the topic.
thingTopic . save ({
success : function ( theTopic ) {
// Subscribe to the topic.
thing . pushSubscription (). subscribe ( thingTopic , {
success : function ( pushSubscription ) {
// Do something.
},
failure : function ( pushSubscription , error ) {
// Handle the errror.
}
});
},
failure : function ( theTopic , error ) {
// Handle the errror.
}
});
},
failure : function ( error ) {
// Load thing failed.
}
});
Getting a list of topics
You can get a list of all topics in the specified thing scope. If there are more than 50 topics, you need to get the topic list in multiple pages with the pagination.
The following is the sample code for getting a list of all topics in a thing scope.
Android
try {
// Instantiate a thing by vendor thing ID.
KiiThing thing = KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" );
// Get the first page of the topic list.
KiiListResult < KiiTopic > result = thing . listTopics ();
for ( KiiTopic topic : result . getResult ()) {
// Do something.
}
if ( result . hasNext ()) {
// Get the next page of the topic list.
result = thing . listTopics ( result . getPaginationKey ());
for ( KiiTopic topic : result . getResult ()) {
// Do something.
}
}
} catch ( IOException e ) {
// Handle the error.
} catch ( AppException e ) {
// Handle the error.
}
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , new KiiCallback < KiiThing >() {
@Override
public void onComplete ( final KiiThing thing , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Get the first page of the topic list.
thing . listTopics ( new KiiCallback < KiiListResult < KiiTopic >>() {
@Override
public void onComplete ( KiiListResult < KiiTopic > result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
for ( KiiTopic topic : result . getResult ()) {
// Do something.
}
if ( result . hasNext ()) {
// Get the next page of the topic list.
thing . listTopics ( result . getPaginationKey (), new KiiCallback < KiiListResult < KiiTopic >>() {
@Override
public void onComplete ( KiiListResult < KiiTopic > result , Exception e ) {
// Do something.
}
});
}
}
});
}
});
This is the brief explanation of the sample code:
Execute the listTopics
method to get a list of topics.
Execute the getResult
method to get the topic list as a List
.
Execute the hasNext
method to check if there exist more topics to get. If there are ones, execute the getPaginationKey
to get a key and execute the listTopics
again with this key.
iOS
Swift:
var resultObj : KiiListResult
do {
// Instantiate a thing by vendor thing ID.
let aThing = try KiiThing . loadSynchronous ( withThingID : "th.id123455" )
// Get the first page of the topic list.
resultObj = try aThing . listTopicsSynchronous ()
} catch ( let error as NSError ){
// Handle the error.
return
}
for topic in ( resultObj . results as! [ KiiTopic ]){
// Do something.
}
if resultObj . hasNext {
// Get the next page of the topic list.
resultObj = try! Kii . listTopicsSynchronous ( resultObj . paginationKey ! , error : ())
for topic in ( resultObj . results as! [ KiiTopic ]){
// Do something.
}
}
// Instantiate a thing by vendor thing ID.
KiiThing . load ( withThingID : "th.id123455" , block : { ( thing : KiiThing ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
// Get the first page of the topic list.
thing !. listTopics { ( resultObj : KiiListResult ?, callerObject : Any ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
for topic in ( resultObj !. results as! [ KiiTopic ]){
// Do something.
}
if resultObj !. hasNext {
// Get the next page of the topic list.
thing !. listTopics ( resultObj !. paginationKey , block : { ( resultObj : KiiListResult ?, callerObject : Any ?, error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
for topic in ( resultObj !. results as! [ KiiTopic ]){
// Do something.
}
})
}
}
})
Objective-C:
NSError * error = nil ;
// Instantiate a thing by vendor thing ID.
KiiThing * aThing = [ KiiThing loadSynchronousWithThingID : @"th.id123455" error : & error ];
// Get the first page of the topic list.
KiiListResult * resultObj = [ aThing listTopicsSynchronous : & error ];
if ( error ){
// Handle the error.
return ;
}
[ resultObj . results enumerateObjectsUsingBlock : ^ ( KiiTopic * topic , NSUInteger idx , BOOL * stop ) {
// Do something.
}];
if ( resultObj . hasNext ){
// Get the next page of the topic list.
resultObj = [ aThing listTopicsSynchronous : resultObj . paginationKey error : & error ];
[ resultObj . results enumerateObjectsUsingBlock : ^ ( KiiTopic * topic , NSUInteger idx , BOOL * stop ) {
// Do something.
}];
}
// Instantiate a thing by vendor thing ID.
[ KiiThing loadWithThingID : @"th.id123455"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error ){
// Handle the error.
return ;
}
// Get the first page of the topic list.
[ thing listTopics : ^ ( KiiListResult * resultObj , id callerObject , NSError * error ) {
if ( error ){
// Handle the error.
return ;
}
[ resultObj . results enumerateObjectsUsingBlock : ^ ( KiiTopic * topic , NSUInteger idx , BOOL * stop ) {
// Do something.
}];
if ( resultObj . hasNext ){
// Get the next page of the topic list.
[ thing listTopics : ^ ( KiiListResult * resultObj , id callerObject , NSError * error ) {
[ resultObj . results enumerateObjectsUsingBlock : ^ ( KiiTopic * topic , NSUInteger idx , BOOL * stop ) {
// Do something.
}];
}];
}
}];
}];
This is the brief explanation of the sample code:
Execute the listTopicsSynchronous
method to get a list of topics.
Execute the [KiiListResult results]
method to get the topic list as a NSArray
.
Execute the [KiiListResult hasNext]
method to check if there exist more topics to get. If there are ones, execute the getPaginationKey
to get a key and execute the listTopicsSynchronous
again with this key.
JavaScript
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , {
success : function ( thing ) {
// Get the first page of the topic list.
thing . listTopics ({
success : function ( topicList , nextPaginationKey ) {
for ( var i = 0 ; i < topicList . length ; i ++ ){
// Do something.
var topic = topicList [ i ];
}
if ( nextPaginationKey != null ) {
// Get the next page of the topic list.
thing . listTopics ({
success : function ( topicList , nextPaginationKey ) {
// Do something.
},
failure : function ( errorString ) {
console . log ( "Error listing topics: " + errorString );
}
}, nextPaginationKey );
}
},
failure : function ( errorString ) {
console . log ( "Error listing topics: " + errorString );
}
);
},
failure : function ( error ) {
// Handle the error.
}
});
This is the brief explanation of the sample code:
Execute the loadWithVendorThingID
method to get an instance of KiiThing.
Execute the listTopics
method to get a list of topics.
Check the nextPaginationKey
to determine if there exist more topics to get. If there are ones, execute the listTopics
method again with the nextPaginationKey
set as a parameter.