Search code examples
iosxcodedelegatesobjective-c-blocksquickblox

How can I wait for delegate to complete before returning completion handler in method?


I have a method which performs an action.

- (void)mutualDeleteDialog:(QBChatDialog *)dialog success:(void (^) ())successBlock failure:(void (^)(NSError *))failureBlock {
  QBChatMessage *deleteMessage = [self generateDeleteDialogMessage:dialog];
  [self sendMessage:deleteMessage success:^{
    [QBChat deleteDialogWithID:dialog.ID delegate:self];
  } failure:^(NSError *error) {
    failureBlock(error);
  }];

The deleteDialogWithID method calls a third party service and calls a delegate method when complete. When this delegate method is called I want to return the success/failure block to the caller of my original method...is this possible and how can I do it?

i.e.

//Delegate Method
- (void)completedWithResult:(QBResult *)result {
  successBlock();
}

Solution

  • If you can guarantee that there's exactly one of these at a time, you could add a property to your class:

    @property (copy) void(^successBlock)();
    

    and then in mutualDeleteDialog:

    self.successBlock = successBlock;
    

    and then in completedWithResult:

    self.successBlock();
    

    This is pretty unconventional. You may want to reevaluate what it is you're trying to do. There might be a better way.