Thing は 識別子 として Thing ベンダーが割り当てた ID(vendorThingID)と Kii Cloud が発行した ID(thingID)を持ちます。Thing はこれらのいずれかの識別子を使って取得できます。
また、一旦取得した Thing は Bundle に保存することで、後にアクティビティが新たに起動した際に復元できます。
Thing の取得と保存
vendorThingID を指定して Thing を取得
以下、Kii Cloud 上で作成済みの Thing を vendorThingID 指定で取得する例を挙げます。
Android
try {
// Instantiate a thing by vendor thing ID.
KiiThing thing = KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" );
// Create a bundle and save the thing.
Bundle bundle = new Bundle ();
bundle . putParcelable ( "my_thing" , thing );
} 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 bundle and save the thing.
Bundle bundle = new Bundle ();
bundle . putParcelable ( "my_thing" , thing );
}
});
この例では、後に復元を行うために Thing を Bundle に保存しています。Thing のインスタンスは Parcelable
を実装しているため Bundle
に保存することができます。復元方法については こちら を参照してください。
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
}
// Instantiate a thing by vendor thing ID.
KiiThing . load ( withVendorThingID : "rBnvSPOXBDF9r29GJeGS" ) { ( thing , 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 ;
}
// Instantiate a thing by vendor thing ID.
[ KiiThing loadWithVendorThingID : @"rBnvSPOXBDF9r29GJeGS"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
JavaScript
// Instantiate a thing by vendor thing ID.
KiiThing . loadWithVendorThingID ( "rBnvSPOXBDF9r29GJeGS" , {
success : function ( thing ) {
// Do something.
},
failure : function ( error ) {
// Handle the error.
}
});
thingID を指定して Thing を取得
以下、Kii Cloud 上で作成済みの Thing を thingID 指定で取得する例を挙げます。
Android
try {
// Instantiate a thing by thing ID.
KiiThing thing = KiiThing . loadWithThingID ( "78924c8vy2984298vvc2" );
// Create a bundle and save the thing.
Bundle bundle = new Bundle ();
bundle . putParcelable ( "my_thing" , thing );
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Instantiate a thing by thing ID.
KiiThing . loadWithThingID ( "78924c8vy2984298vvc2" , new KiiCallback < KiiThing >() {
@Override
public void onComplete ( KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Create a bundle and save the thing.
Bundle bundle = new Bundle ();
bundle . putParcelable ( "my_thing" , thing );
}
});
この例では、後に復元を行うために Thing を Bundle に保存しています。Thing のインスタンスは Parcelable
を実装しているため Bundle
に保存することができます。復元方法については こちら を参照してください。
iOS
Swift:
let thing : KiiThing
do {
// Instantiate a thing by thing ID.
thing = try KiiThing . loadSynchronous ( withThingID : "th.rBnvSPOXBDF9r29GJeGS" )
} catch ( let error as NSError ){
// Handle the error.
return
}
// Instantiate a thing by thing ID.
KiiThing . load ( withThingID : "th.rBnvSPOXBDF9r29GJeGS" ) { ( thing , error ) -> Void in
if error != nil {
// Handle the error.
return
}
}
Objective-C:
NSError * error = nil ;
// Instantiate a thing by thing ID.
KiiThing * thing = [ KiiThing loadSynchronousWithThingID : @"th.rBnvSPOXBDF9r29GJeGS"
error : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Instantiate a thing by thing ID.
[ KiiThing loadWithThingID : @"th.rBnvSPOXBDF9r29GJeGS"
block : ^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
JavaScript
// Instantiate a thing by thing ID.
KiiThing . loadWithThingID ( "78924c8vy2984298vvc2" , {
success : function ( thing ) {
// Do something.
},
failure : function ( error ) {
// Handle the error.
}
});
自分がオーナーになっている Thing の一覧を取得
ログイン中のユーザー、またはログイン中のユーザーがメンバーのグループがオーナーになっている Thing の一覧を取得する例を挙げます。下記の例はこの条件に該当する Thing をすべて取得しますが、KiiThingQuery
クラスの setThingType()
メソッドなどを使うと Thing タイプでフィルターすることができます。
Android
// Get the current user.
KiiUser user = KiiUser . getCurrentUser ();
// Create a query for things owned by the current user or the groups that the current user is a member of.
KiiThingQuery query = new KiiThingQuery ( user , user . memberOfGroups ());
// Query things.
KiiThingQueryResult result = KiiThing . query ( query );
List < KiiThing > things = result . getResult ();
for ( KiiThing thing : things ) {
// Do something.
}
// Get the current user.
KiiUser user = KiiUser . getCurrentUser ();
// Get a list of groups that the current user is a member of.
user . memberOfGroups ( new KiiUserCallBack () {
@Override
public void onMemberOfGroupsCompleted ( int token , KiiUser user , List < KiiGroup > groupList , Exception exception ) {
if ( exception != null ) {
// Handle the error.
return ;
}
// Create a query for things owned by the current user or the groups that the current user is a member of.
KiiThingQuery query = new KiiThingQuery ( user , groupList );
// Query things.
KiiThing . query ( query , new KiiCallback < KiiThingQueryResult >() {
@Override
public void onComplete ( KiiThingQueryResult result , Exception e ) {
List < KiiThing > things = result . getResult ();
for ( KiiThing thing : things ) {
// Do something.
}
}
});
}
});
検索結果の処理方法は KiiObject の検索 と同じです。必要に応じてページネーションの処理を行います。
iOS
Swift:
// Get the current user.
let user : KiiUser = KiiUser . current () !
do {
// Get a list of groups that the current user is a member of.
let groups = try user . memberOfGroupsSynchronous () as! [ KiiGroup ]
// Create a query for things owned by the current user or the groups that the current user is a member of.
let query = KiiThingQuery ( user , groups : groups )
// Query things.
let queryResult = try KiiThing . querySynchronous ( query )
for thing in queryResult . results ! {
// Do something.
}
} catch ( let error as NSError ){
// Handle the error.
return
}
// Get a list of groups that the current user is a member of.
KiiUser . current ()? . memberOfGroups ({ ( user , groups , error ) in
// Create a query for things owned by the current user or the groups that the current user is a member of.
let query = KiiThingQuery ( user , groups : ( groups as! [ KiiGroup ]))
// Query things.
KiiThing . query ( query , block : { ( queryResult , query , error ) in
if error != nil {
// Handle the error.
return
}
for thing in queryResult . results ! {
// Do something.
}
})
})
Objective-C:
NSError * error = nil ;
// Get the current user.
KiiUser * user = [ KiiUser currentUser ];
// Get a list of groups that the current user is a member of.
NSArray * groups = [ user memberOfGroupsSynchronous : & error ];
// Create a query for things owned by the current user or the groups that the current user is a member of.
KiiThingQuery * query = [ KiiThingQuery thingQuery : user groups : groups ];
// Query things.
KiiThingQueryResult * queryResult = [ KiiThing querySynchronous : query error : & error ];
NSArray < KiiThing * > * results = queryResult . results ;
for ( KiiThing * thing in results ){
// Do something.
}
// Get the current user.
KiiUser * user = [ KiiUser currentUser ];
// Get a list of groups that the current user is a member of.
[ user memberOfGroupsWithBlock : ^ ( KiiUser * _Nonnull user , NSArray * _Nullable groups , NSError * _Nullable error ) {
// Create a query for things owned by the current user or the groups that the current user is a member of.
KiiThingQuery * query = [ KiiThingQuery thingQuery : user groups : groups ];
// Query things.
[ KiiThing query : query block : ^ ( KiiThingQueryResult * _Nullable queryResult , KiiThingQuery * _Nullable query , NSError * _Nullable error ) {
NSArray < KiiThing * > * results = queryResult . results ;
for ( KiiThing * thing in results ){
// Do something.
}
}];
}];
JavaScript
// Get a list of groups that the current user is a member of.
KiiUser . getCurrentUser (). memberOfGroups ({
success : function ( user , groupList ) {
// Create a query for things owned by the current user or the groups that the current user is a member of.
var query = KiiThingQuery . thingQuery ( user , groupList );
// Query things.
KiiThing . executeQuery ( query , {
success : function ( result ) {
var resultSet = result . getResult ();
for ( var i = 0 ; i < resultSet . length ; i ++ ) {
var thing = resultSet [ i ];
// Do something.
}
if ( result . hasNext ()) {
// Get the next page of the result.
result . getNextResult ({
success : function ( result ) {
// Handle the next page of the result.
var resultSet = result . getResult ();
},
failure : function ( err ) {
// Handle the error.
}
});
}
},
failure : function ( err ) {
// Handle the error.
}
});
},
failure : function ( theUser , errorString ) {
// Handle the error.
}
});
Thing のリフレッシュ
以下、サーバーから最新の Thing の情報を取得して、KiiThing
インスタンスに反映するサンプルコードです。
loadWithVendorThingID()
メソッドや loadWithThingID()
メソッドを使って作成した KiiThing
インスタンスには、すでに最新の情報が反映されているため、この処理を実行する必要はありません。
Android
// Instantiate a thing.
KiiThing thing = ...
try {
// Refresh the thing.
thing . refresh ();
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Instantiate a thing.
KiiThing thing = ...
// Refresh the thing.
thing . refresh ( new KiiCallback < KiiThing >() {
@Override
public void onComplete ( KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
}
});
iOS
Swift:
// Instantiate a thing.
let thing = ... : KiiThing
do {
// Refresh the thing.
try thing . refreshSynchronous ()
} catch ( let error as NSError ){
// Handle the error.
return
}
// Instantiate a thing.
let thing = ... : KiiThing
// Refresh the thing.
thing . refresh () { ( thing , error ) -> Void in
if error != nil {
// Handle the error.
return
}
}
Objective-C:
// Instantiate a thing.
KiiThing * thing = ...;
NSError * error = nil ;
// Refresh the thing.
[ thing refreshSynchronous : & error ];
if ( error != nil ) {
// Handle the error.
return ;
}
// Instantiate a thing.
KiiThing * thing = ...;
// Refresh the thing.
thing refresh :^ ( KiiThing * thing , NSError * error ) {
if ( error != nil ) {
// Handle the error.
return ;
}
}];
JavaScript
// Instantiate a thing.
var thing = ...;
// Refresh the thing.
thing . refresh ({
success : function ( thing ) {
// Do something.
},
failure : function ( error ) {
// Handle the error.
}
});
Thing の復元(Android のみ)
以下、Bundle に保存していた Thing を復元し、Kii Cloud 上にある最新の状態に更新するサンプルコードです。
Android
try {
// Restore a thing from a bundle.
KiiThing thing = ( KiiThing ) bundle . getParcelable ( "my_thing" );
// Refresh the thing.
thing . refresh ();
} catch ( AppException e ) {
// Handle the error.
} catch ( IOException e ) {
// Handle the error.
}
// Restore a thing from a bundle.
KiiThing thing = ( KiiThing ) bundle . getParcelable ( "my_thing" );
// Refresh the thing.
thing . refresh ( new KiiCallback < KiiThing >() {
@Override
public void onComplete ( KiiThing result , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
}
});
基本手順は以下のとおりです。
Bundle
から保存していた Thing のインスタンスを取得します。
refresh
メソッドを実行して Kii Cloud より最新の Thing を取得します。