正在学习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;
}
Asynchronous:
Sync:
Add the shape of the factory method to a block, and the block callback objectID
dispatch_semaphore_t, wait before return.
You deserve to have GCD group. If you use swift, an @escaping block will do it. .