正在学习Leancloud,想用一个工厂方法来封装查询代码,但是不能return block里的值,直接return objectID是空值,求指教,谢谢!
+ (NSString *)getObjectIDbyUsername:(NSString *)_username
{
__block NSString *objectID = NULL;
NSString *username = [NSString stringWithFormat:@"^%@$",_username];
AVQuery *query = [AVQuery queryWithClassName:@"_User"];
[query whereKey:@"username" containsString:username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
objectID = [objects valueForKey:@"objectId"];
}];
return objectID;
}
异步:
同步:
把工厂方法的形参加一个block, block 回调objectID
dispatch_semaphore_t, 在return前一直wait.
GCD group你值得拥有,假如用swift的话一个@escaping block就搞定了。。