我写的是一朋友圈的功能,需要显示状态的点赞数和评论数,但是点赞和评论的接口都是 block异步获取,在格式化 model的时候无法正常获取,所以想请求解决方法,非常感谢。
-(HxAlbumModel *)factoryModel:(BmobObject *)obj statusType:(StatusType)type{
hxAlbum *albumOBj = [[hxAlbum alloc] initFromBmobObject:obj];
NSMutableArray* photos=[NSMutableArray array];
NSArray * albumPhotos = [obj objectForKey:@"albumPhotos"];
if (albumPhotos.count !=0) {
for(NSString * photoUrl in albumPhotos){
hxAlbumPhoto *photo = [hxAlbumPhoto photoWithOriginUrl:photoUrl thumbnailUrl:[NSString stringWithFormat:@"%@!/format/png",photoUrl]];
[photos addObject:photo];
}
}
//这里格式化 model数据,其中的commentArray和digArray 需要从 block块内异步获取的,这里我就不知道如何操作比较好,所以想问问。非常感谢。
HxAlbumModel*model = [HxAlbumModel hxAlbuMmodelWithType:Status_ENUM_TYPE fromUser:albumOBj.creator bmobOject:albumOBj avatar:[albumOBj.creator objectForKey:KEY_AVATAR] time:[albumOBj.createdAt stringWithFormat:@"MM-dd HH:mm"] statusID:albumOBj.objectId statusCreateAt:[NSString stringWithFormat:@"%.3lf",[albumOBj.createdAt timeIntervalSince1970]] digArray:albumOBj.toDigs commentArray:albumOBj.toStatusComments contentText:albumOBj.timeline photosArray:photos statusContentRelatedType:nil];
return model;
}
//查询点赞的关联表
- (void)queryDigsRelation:(NSString*)statusID toDigs:(void(^)(NSMutableArray * toDigs))sucessed{
//关联对象表
BmobQuery *bquery = [BmobQuery queryWithClassName:@"_User"];
//需要查询的列
BmobObject *post = [BmobObject objectWithoutDataWithClassName:@"hxAlbum" objectId:statusID];
[bquery whereObjectKey:@"toDigs" relatedTo:post];
[bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
if (error) {
NSLog(@"%@",error);
}else{
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
BmobObject *user = obj;
NSLog(@"===%@",[user objectForKey:@"username"]);
}];
sucessed([array copy]);
}
}];
}
@Stephanie
这是用这异步函数进行尝试编写的,但是我感觉有些地方还需要优化一下。麻烦大佬看下,看看是不是这样搞的,非常感谢。🙏
#pragma mark FindVC下拉刷新
- (void)downloadMoreDataSuccessed:(void(^)(NSMutableArray * modelarray))sucessed
downloadModel:(NSMutableArray*)modelArray{
NSMutableArray * array = [[NSMutableArray alloc]init];
BmobQuery *query = [BmobQuery queryWithClassName:@"hxAlbum"];
if (modelArray.count != 0) {
HxAlbumModel * model = [modelArray firstObject];
NSDictionary *condiction2 = @{@"createdAt":@{@"$gt":@{@"__type": @"Date", @"iso":[self getDateStr:model.createdAts isDown:YES]}}};
NSArray *condictonArray = @[condiction2];
//作用就是查询创建时间在2014年7月15日到2014年10月15日之间的数据
[query addTheConstraintByAndOperationWithArray:condictonArray];
};
[query orderByDescending:@"createdAt"];
[query setLimit:10];
[query includeKey:@"creator,albumPhotos"];
query.cachePolicy = kBmobCachePolicyNetworkElseCache;
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) {
if (results.count != 0) {
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("comw", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
dispatch_sync(queue, ^{
[results enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
dispatch_group_enter(group);
[self factoryModel:obj statusType:Status_ENUM_TYPE blockModel:^(HxAlbumModel * model) {
[array addObject:model];
dispatch_group_leave(group);
NSLog(@"00");
}];
}];
});
});
dispatch_group_notify(group, queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"01");
sucessed(array);
});
});
}
}else{
[BmobAlbumCloudTools errorlog:error log:@"=====2FindVC获取消息失败=="];
NSLog(@"下拉刷新,服务器返回错误:%@",error);
sucessed(array);
}
}];
}
-(void)factoryModel:(BmobObject *)obj statusType:(StatusType)type blockModel:(void(^)(HxAlbumModel *))sucessed{
hxAlbum *albumOBj = [[hxAlbum alloc] initFromBmobObject:obj];
NSMutableArray* photos=[NSMutableArray array];
NSArray * albumPhotos = [obj objectForKey:@"albumPhotos"];
if (albumPhotos.count !=0) {
for(NSString * photoUrl in albumPhotos){
hxAlbumPhoto *photo = [hxAlbumPhoto photoWithOriginUrl:photoUrl thumbnailUrl:[NSString stringWithFormat:@"%@!/format/png",photoUrl]];
[photos addObject:photo];
}
}
__block NSMutableArray *temptoDigs;
__block NSMutableArray *temptoComments;
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("com.gcd-group.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
dispatch_sync(queue, ^{
[self queryDigsRelation:albumOBj.objectId toDigs:^(NSMutableArray *toDigs) {
temptoDigs = toDigs;
NSLog(@"22 %@",[NSThread currentThread]);
NSLog(@"===%lu",(unsigned long)temptoDigs.count);
dispatch_group_leave(group);
}];
});
});
dispatch_group_notify(group, queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
HxAlbumModel*model = [HxAlbumModel hxAlbuMmodelWithType:Status_ENUM_TYPE fromUser:albumOBj.creator bmobOject:albumOBj avatar:[albumOBj.creator objectForKey:KEY_AVATAR] time:[albumOBj.createdAt stringWithFormat:@"MM-dd HH:mm"] statusID:albumOBj.objectId statusCreateAt:[NSString stringWithFormat:@"%.3lf",[albumOBj.createdAt timeIntervalSince1970]] digArray:temptoDigs commentArray:@[] contentText:albumOBj.timeline photosArray:photos statusContentRelatedType:nil];
model.createdAts = obj.createdAt;
sucessed(model);
});
});
}