当我确信两个块代码都已返回时,我如何触发方法?像这样......
// Retrieve Messages Array from Parse
[ParseManager retrieveAllMessagesForShredderUser:(ShredderUser *)[PFUser currentUser] withCompletionBlock:^(BOOL success, NSError *error, NSArray *objects){
self.messagesArray = objects;
}];
// Retrieve MessagesPermissions Array from Parse
[ParseManager retrieveAllMessagePermissionsForShredderUser:(ShredderUser *)[PFUser currentUser] withCompletionBlock:^(BOOL success, NSError *error, NSArray *objects){
self.messagePermissionsArray = objects;
}];
-(void)methodToRunWhenBothBlocksHaveReturned{
}
答案 0 :(得分:4)
如果你可以保证在同一个线程(即UI线程)上执行这些块,那么使用__block
变量就可以很简单了。
-(void)yourMethod {
__block int count = 0;
[ParseManager retrieveAllMessagesForShredderUser:(ShredderUser *)[PFUser currentUser] withCompletionBlock:^(BOOL success, NSError *error, NSArray *objects){
self.messagesArray = objects;
count++;
if (count == 2) {
[self methodToRunWhenBothBlocksHaveReturned];
}
}];
[ParseManager retrieveAllMessagePermissionsForShredderUser:(ShredderUser *)[PFUser currentUser] withCompletionBlock:^(BOOL success, NSError *error, NSArray *objects){
self.messagePermissionsArray = objects;
count++;
if (count == 2) {
[self methodToRunWhenBothBlocksHaveReturned];
}
}];
}
-(void)methodToRunWhenBothBlocksHaveReturned{
}
如果你没有相同的线程保证,你可以使用lock来确保变量的增量(和2的比较)是原子的。