关于Bmob的使用

1、向Bmob里边存入图片数组

- (void)right:(UIBarButtonItem *)sender{
    
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        //进行操作
       friends = [BmobObject objectWithClassName:@"Friends"];
        [friends setObject:bUser.username forKey:@"name"];
         NSString *iconURLString = [[bUser objectForKey:@"imageURL"]lastObject];
        
        [friends setObject:iconURLString forKey:@"iconImageName"];
        
        [friends setObject:_writeNewsView.text forKey:@"newsContent"];
        
        if (pictureData.count>1) {
            
            NSMutableArray * dicArr = [NSMutableArray array];
            int num;
            for (num = 0; num < pictureData.count; num++) {
                NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"image.jpg",@"filename",[pictureData objectAtIndex:num],@"data", nil];
                [dicArr addObject:dic];
                
            }
            [BmobProFile uploadFilesWithDatas:dicArr resultBlock:^(NSArray *filenameArray, NSArray *urlArray, NSArray *bmobFileArray, NSError *error) {
                if (error) {
                    NSLog(@"错误");
                }else{
                    NSMutableArray * bmobFileArr = [NSMutableArray array];
                    for (BmobFile *file in bmobFileArray) {
                        
                        [bmobFileArr addObject:file.url];
                    }
                    [friends addObjectsFromArray:bmobFileArr forKey:@"pictureArray"];
                    [friends saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        if (isSuccessful) {
                            
                            [self showAlectControllerWithTitle:@"提示" Message:@"发表成功"];
                            [self.navigationController pushViewController:[WriteNewsViewController new] animated:YES];
                        }else{
                            
                            [self showAlectControllerWithTitle:@"提示" Message:@"发表失败"];
                        }
                        
                    }];
                    
                }
                
            } progress:^(NSUInteger index, CGFloat progress) {
                
            }];
            
        }else{
            
            NSLog(@"budayu1");
        }
        
    }else{
        //对象为空时,可打开用户注册界面
        [self showAlectControllerWithTitle:@"提示" Message:@"请登录账号"];
    }
            
    
    NSLog(@"%@,%lu",_writeNewsView.text,(unsigned long)pictureData.count);
    

}

2、读取多条数据

- (void)loadData{

    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Friends"];
    bquery.limit = 2;
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            NSLog(@"%@",[obj objectId]);
            
            [bquery getObjectInBackgroundWithId:[obj objectId] block:^(BmobObject *object,NSError *error){
                if (error){
                    //进行错误处理
                    NSLog(@"....");
                }else{
                    if (object) {
                        
                        NSLog(@"%@",[object objectForKey:@"newsContent"]);
                        NSLog(@"%@",[object objectForKey:@"iconImageName"]);
                        [list addObject:[object objectForKey:@"name"]];
                        [list addObject:[object objectForKey:@"newsContent"]];
                        [list addObject:[object objectForKey:@"iconImageName"]];
                        
                        [list addObject:[object objectForKey:@"pictureArray"]];
                        
                        [list addObject:[obj objectForKey:@"createdAt"]];
                        
                        [dataList addObject:list];
                        list = [NSMutableArray array];
                        NSLog(@"...%@",dataList);
                        
                        [self.dataArray addObjectsFromArray:[self createModelWithCount]];
                    }
                }
                [self.tableView reloadData];
            }];
        }
        
    }];

}

3、复合查询读取名字和创建时间

BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Friends"];
        [bquery whereKey:@"name" equalTo:alloc.nameLabel.text];
        BmobQuery   *bquery1 = [BmobQuery queryWithClassName:@"Friends"];
        [bquery1 whereKey:@"createdAt" equalTo:alloc.timeLabel.text];
        
        BmobQuery   *main = [BmobQuery queryWithClassName:@"Friends"];
        [main add:bquery];
        [main add:bquery1];
        [main orOperation];
        [main findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            for (BmobObject *obj in array) {
                //打印playerName
                NSLog(@"...%@",[obj objectForKey:@"createdAt"]);
                NSLog(@"obj.playerName = %@", [obj objectForKey:@"name"]);
                createTime = [obj objectForKey:@"createdAt"];
                userName = [obj objectForKey:@"name"];
            }
        }];

4、添加多条数据

- (void)sure:(UIButton *)sender{

    NSLog(@"....%@",commentTextView.text);
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        //进行操作
        BmobObject *comment = [BmobObject objectWithClassName:@"CommunityComment"];
        [comment setObject:bUser.username forKey:@"name"];
        NSString *iconURLString = [[bUser objectForKey:@"imageURL"]lastObject];
        
        [comment setObject:iconURLString forKey:@"iconImageName"];
        [comment setObject:createTime forKey:@"createTime"];
        [comment setObject:userName forKey:@"userName"];
        
        [comment setObject:commentTextView.text forKey:@"comment"];
        [comment saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                
                [self showAlectControllerWithTitle:@"提示" Message:@"评论成功"];
                [self.navigationController pushViewController:[WriteNewsViewController new] animated:YES];
            }else{
                
                [self showAlectControllerWithTitle:@"提示" Message:@"评论失败"];
            }
            
        }];
        

        
    }else{
        //对象为空时,可打开用户注册界面
        [self showAlectControllerWithTitle:@"提示" Message:@"请登录账号"];
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,391评论 11 349
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,087评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,242评论 6 13
  • 2017.09.14. 星期四 晴天 今天晚上我洗澡的时候,本来不用洗头的,可是我为了玩憋气所以就洗头了。 先...
    王紫杨阅读 478评论 0 9
  • 这是来自我手机的一张截屏!生活是需要体验的,不要只是当一个存在者,噢!不,可能连存在者也不是! 我是...
    市中杂记阅读 446评论 2 2