Call a completion block from a delegate method

Anthony Castelli

It's hard to explain, but basically what I am trying to do is call a completion handler in a block-based method from a delegate method.

I have the method where I call the upload function.

[[UploadManager sharedManager] uploadFile:@"/Path/To/Image.png" success:^(NSDictionary *response) {
    NSLog(@"Uploaded");
}];

Inside of the UpLoad manager, the method performs all the necessary actions to upload the file.

There is a delegate method that I want to call the success block from.

- (void)fileDidUploadAndReceiveData:(NSData *)data {
    NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
    // Call the completion method
    success(response);
}

success is defined in the uploadFile method. How would I go about calling this completion handler?

I wrote this late at night so if it doesn't make sense, let me know.

Thanks

danh

Declare a property that is a copy of the block:

@property(nonatomic, copy) void (^completionBlock)(NSDictionary *);

Assign it in uploadFile:

- (void)uploadFile:(NSString *)url success:(void (^)(NSDictionary *))completionBlock {
    self.completionBlock = completionBlock;
    // ...

Then call it whenever you want:

if (self.completionBlock) self.completionBlock(someDictionary);
self.completionBlock = nil;  // see below

Remember that, if you don't need the block again (which you probably don't since the download is complete) that it's a good practice to nil out your copy of the block. This way, if the caller refers to the download manager within the block, you'll break the retain cycle for him (the block would retain the download manager which retains the block).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call a completion block from a delegate method

From Dev

Call a method completion block after button is tapped

From Dev

How to call SuperClass delegate method from SubClass delegate method

From Dev

how to call completion block?

From Dev

how to call completion block?

From Dev

Call app delegate method from view controller

From Dev

Call ViewController method from App Delegate

From Dev

Adding a completion handler on a delegate method

From Dev

Is it possible to call a block completion handler from another function in iOS?

From Dev

call image picker controller delegate method from another method

From Dev

Unable to call delegate method

From Dev

Method does not reach Completion Block

From Dev

How to block call overridden method from Child

From Dev

Call ViewControllers Method or Segue from App Delegate (Swift)

From Dev

Call method in another thread on completion

From Dev

Wait for async task to finish completion block before returning in app delegate

From Dev

Call delegate method on item select

From Dev

Call operation in completion block without memory leak

From Dev

Return object for a method inside completion block

From Dev

Performance of reflection method call vs delegate call

From Dev

Performance of reflection method call vs delegate call

From Dev

Swift syntax for block with completionHandler... in delegate method

From Dev

Swift syntax for block with completionHandler... in delegate method

From Dev

Objective-C - block loop from continuing until delegate method is called

From Dev

how to call block method from magento 1 template

From Dev

is it good practice to call a method from Exception block in java?

From Dev

Completion block in Swift from Objective C

From Dev

Access delegate object from a method

From Dev

Catching a delegate method call in the originating subclass

Related Related

  1. 1

    Call a completion block from a delegate method

  2. 2

    Call a method completion block after button is tapped

  3. 3

    How to call SuperClass delegate method from SubClass delegate method

  4. 4

    how to call completion block?

  5. 5

    how to call completion block?

  6. 6

    Call app delegate method from view controller

  7. 7

    Call ViewController method from App Delegate

  8. 8

    Adding a completion handler on a delegate method

  9. 9

    Is it possible to call a block completion handler from another function in iOS?

  10. 10

    call image picker controller delegate method from another method

  11. 11

    Unable to call delegate method

  12. 12

    Method does not reach Completion Block

  13. 13

    How to block call overridden method from Child

  14. 14

    Call ViewControllers Method or Segue from App Delegate (Swift)

  15. 15

    Call method in another thread on completion

  16. 16

    Wait for async task to finish completion block before returning in app delegate

  17. 17

    Call delegate method on item select

  18. 18

    Call operation in completion block without memory leak

  19. 19

    Return object for a method inside completion block

  20. 20

    Performance of reflection method call vs delegate call

  21. 21

    Performance of reflection method call vs delegate call

  22. 22

    Swift syntax for block with completionHandler... in delegate method

  23. 23

    Swift syntax for block with completionHandler... in delegate method

  24. 24

    Objective-C - block loop from continuing until delegate method is called

  25. 25

    how to call block method from magento 1 template

  26. 26

    is it good practice to call a method from Exception block in java?

  27. 27

    Completion block in Swift from Objective C

  28. 28

    Access delegate object from a method

  29. 29

    Catching a delegate method call in the originating subclass

HotTag

Archive