How to store a Closure completion handler to call later?

Jordan H

I'm working on converting this In-App Purchase tutorial into Swift and have come across an issue trying to save the completion handler. We need to store this completion handler so that it can be called later. I cannot figure out how to store a copy of the closure from the arguments. Specifically, I believe I need to copy it but Xcode states this object doesn't have a copy function.

I've defined it like so:

typealias RequestProductsCompletionHandler = (success: Bool, products: NSArray) -> Void

Here I declare a property for it:

var completionHandler: RequestProductsCompletionHandler?

Then this is where I need to store the passed in completion handler into my property:

func requestProductsWithCompletionHandler(completionBlock: RequestProductsCompletionHandler) -> Void {
    //self.completionHandler = completionBlock.copy() //problem: RequestProductsCompletionHandler does not have a member named copy
}

This is how it was done in the Obj-C tutorial:

typedef void (^RequestProductsCompletionHandler)(BOOL success, NSArray * products);

RequestProductsCompletionHandler _completionHandler;

- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler {
    _completionHandler = [completionHandler copy];
}

And later it is used like so:

_completionHandler(YES, skProducts);
_completionHandler = nil;

EDIT: I've removed the .copy() to prevent the error (also had to make the NSArray optional so that I can set it to nil later). My question is, will it work as expected without explicitly copying it? I don't believe it will because Apple stated closures are reference types. This will store a reference to the original closure which I don't want to do. How does one enforce a copy?

tsafrir

Yes. It will work as expected.

Whenever you call requestProductsWithCompletionHandler you create a closure and pass it to that function. And as you mentioned, when you set completionHandler you actually set it to be a reference to the given closure.

In Objective C, to store the block as ivar you had to copy a block. That's because a block first residing on the stack memory where it was defined. And copy moved it to the heap memory so it can be used

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call this completion handler?

From Dev

How to add completion handler/closure for 'animated: true'

From Dev

Swift: How do I store an array of object by reference from completion handler (closure)?

From Dev

How to pass (optional) completion handler closure to transitionFromViewController in Swift?

From Dev

Store setTimeout and call it later

From Dev

Swift completion handler - escaping trailing closure

From Dev

How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

From Dev

How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

From Dev

Store Completion Handler in Variable iOS (Objective c)

From Dev

Calling completion handler within nested closure to stop recursive function

From Dev

Call Swift function with completion handler in objective c

From Dev

Call Swift completion handler in objective c

From Dev

Unable to make network call from completion handler?

From Dev

Unable to make network call from completion handler?

From Dev

Call Swift function with completion handler in objective c

From Dev

How to store a generic closure?

From Dev

How to cancel a completion handler in iOS

From Dev

How to reset a Completion Handler in Swift

From Dev

How do I store one completion handler in a class so that all methods could use it?

From Dev

how to call completion block?

From Dev

how to call completion block?

From Dev

How to call a closure in a method

From Dev

How to wait for a closure completion before returning a value

From Dev

How to handle with a multiple errors in a completion closure

From Dev

How to store a value for later use?

From Dev

How to write completion handler in Swift with animation.

From Dev

How to pass background task identifier to completion handler?

From Dev

How to properly dismiss modal viewcontroller with completion handler

From Dev

How can a completion handler return to the wrong ViewController?

Related Related

  1. 1

    How to call this completion handler?

  2. 2

    How to add completion handler/closure for 'animated: true'

  3. 3

    Swift: How do I store an array of object by reference from completion handler (closure)?

  4. 4

    How to pass (optional) completion handler closure to transitionFromViewController in Swift?

  5. 5

    Store setTimeout and call it later

  6. 6

    Swift completion handler - escaping trailing closure

  7. 7

    How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

  8. 8

    How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

  9. 9

    Store Completion Handler in Variable iOS (Objective c)

  10. 10

    Calling completion handler within nested closure to stop recursive function

  11. 11

    Call Swift function with completion handler in objective c

  12. 12

    Call Swift completion handler in objective c

  13. 13

    Unable to make network call from completion handler?

  14. 14

    Unable to make network call from completion handler?

  15. 15

    Call Swift function with completion handler in objective c

  16. 16

    How to store a generic closure?

  17. 17

    How to cancel a completion handler in iOS

  18. 18

    How to reset a Completion Handler in Swift

  19. 19

    How do I store one completion handler in a class so that all methods could use it?

  20. 20

    how to call completion block?

  21. 21

    how to call completion block?

  22. 22

    How to call a closure in a method

  23. 23

    How to wait for a closure completion before returning a value

  24. 24

    How to handle with a multiple errors in a completion closure

  25. 25

    How to store a value for later use?

  26. 26

    How to write completion handler in Swift with animation.

  27. 27

    How to pass background task identifier to completion handler?

  28. 28

    How to properly dismiss modal viewcontroller with completion handler

  29. 29

    How can a completion handler return to the wrong ViewController?

HotTag

Archive