Perseus
Perseus

Reputation: 1588

Store completion handler and call success/error later

I have a protocol/function in my class which is below,

func getMovieList(completionHandler: @escaping (Result<[String], Error>) -> Void) { }

When the above method is called, I want to store the completion handler and call the success/error in the latter part.

I tried creating a typealias like below,

typealias AlbumListCompletionHandler = (((Result<[String], Error>)) -> Void)?

And in my class,

var completionHandlerObj: AlbumListCompletionHandler

func getMovieList(completionHandler: @escaping (Result<[String], Error>) -> Void) { 
    completionHandlerObj = completionHandler
    /...
    .../
}

But I wonder how do I call the success/error blocks in completionHandlerObj, kind of struck here. Can anyone help me with this ?

Upvotes: 1

Views: 744

Answers (1)

Tomo Norbert
Tomo Norbert

Reputation: 780

It should work like this

completionHandlerObj(.success(["",""]))
completionHandlerObj(.failure(ErrorObject))

Upvotes: 1

Related Questions