美文网首页
关于Bmob的使用

关于Bmob的使用

作者: CMD独白 | 来源:发表于2016-10-01 18:19 被阅读777次

    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:@"请登录账号"];
        }
        
    }
    

    相关文章

      网友评论

          本文标题:关于Bmob的使用

          本文链接:https://www.haomeiwen.com/subject/miknlttx.html