- (IBAction)onClick:(id)sender {
NSDate *d = [[NSDate alloc]init];
double now = [d timeIntervalSince1970];
NSString *username = [NSString stringWithFormat:@"user-%f", now];
KiiUser *user = [KiiUser userWithUsername:username andPassword:@"1234"];
[user performRegistrationWithBlock:^(KiiUser *user, NSError *error) {
if (error != nil) {
NSLog(@"error on reg: %@", error);
return;
}
KiiObject *obj = [[Kii bucketWithName:@"myBucket34"] createObject];
[obj setObject:@"v1" forKey:@"f1"];
[obj setObject:@"v2" forKey:@"f2"];
[self dumpObj:obj message:@"jsonStr before save"];
[obj saveWithBlock:^(KiiObject *object, NSError *error) {
if (error != nil) {
NSLog(@"error on save: %@", error);
return;
}
[self dumpObj:obj message:@"jsonStr after save"];
[obj refreshWithBlock:^(KiiObject *object, NSError *error) {
if (error != nil) {
NSLog(@"error on refresh: %@", error);
return;
}
[self dumpObj:obj message:@"jsonStr after refresh"];
}];
}];
}];
}
- (void) dumpObj:(KiiObject*)obj message:(NSString*)message{
NSDictionary *dict = [obj dictionaryValue];
NSError *parseError;
NSData *dump = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&parseError];
NSString *jsonStr = [[NSString alloc]initWithData:dump encoding:NSUTF8StringEncoding];
if (parseError != nil) {
NSLog(@"Failed to parse obj: %@", parseError);
}
NSLog(@"%@: %@", message, jsonStr);
}
Output:
2014-04-21 14:11:55.489 ToStringTest[54853:60b] jsonStr before save: {"f1":"v1","f2":"v2"}
2014-04-21 14:11:55.761 ToStringTest[54853:60b] jsonStr after save: {"f1":"v1","dataType":"application\/json","f2":"v2"}
2014-04-21 14:11:55.908 ToStringTest[54853:60b] jsonStr after refresh: {"f1":"v1","dataType":"application\/json","f2":"v2"}