码迷,mamicode.com
首页 > 其他好文 > 详细

Bmob 之 简单使用

时间:2015-12-09 13:40:08      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

1. pod 

  pod ‘BmobSDK‘ 与 pod "BmobSDK" 好像没什么区别

2. 导入

  在AppDelegate中:

#import <BmobSDK/Bmob.h>

[Bmob registerWithAppKey:@"申请的Application ID"];

3. 数据处理

3.1. 添加

//往GameScore表添加一条playerName为小明,分数为78的数据
BmobObject *gameScore = [BmobObject objectWithClassName:@"GameScore"];
[gameScore setObject:@"小明" forKey:@"playerName"];
[gameScore setObject:@78 forKey:@"score"];
[gameScore setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];
[gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
    //进行操作
}];

 

3.2. 获取

//查找GameScore表
BmobQuery   *bquery = [BmobQuery queryWithClassName:@"GameScore"];
//查找GameScore表里面id为0c6db13c的数据
[bquery getObjectInBackgroundWithId:@"0c6db13c" block:^(BmobObject *object,NSError *error){
  if (error){
          //进行错误处理
  }else{
        //表里有id为0c6db13c的数据
      if (object) {
            //得到playerName和cheatMode
          NSString *playerName = [object objectForKey:@"playerName"];
          BOOL cheatMode = [[object objectForKey:@"cheatMode"] boolValue];
          NSLog(@"%@----%i",playerName,cheatMode);
      }
  }
}];

 

3.3. 修改

//查找GameScore表
BmobQuery   *bquery = [BmobQuery queryWithClassName:@"GameScore"];
//查找GameScore表里面id为0c6db13c的数据
[bquery getObjectInBackgroundWithId:@"0c6db13c" block:^(BmobObject *object,NSError *error){
  //没有返回错误
  if (!error) {
      //对象存在
      if (object) {
            BmobObject *obj1 = [BmobObject objectWithoutDatatWithClassName:object.className objectId:object.objectId];
             //设置cheatMode为YES
          [obj1 setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];
          //异步更新数据
          [obj1 updateInBackground];
      }
  }else{
    //进行错误处理
  }
}];

 

3.4. 删除

BmobQuery *bquery = [BmobQuery queryWithClassName:@"GameScore"];
[bquery getObjectInBackgroundWithId:@"0c6db13c" block:^(BmobObject *object, NSError *error){
    if (error) {
        //进行错误处理
    }
    else{
        if (object) {
            //异步删除object
            [object deleteInBackground];
        }
    }
}];

 

Bmob 之 简单使用

标签:

原文地址:http://www.cnblogs.com/SimonGao/p/5032315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!