Heres an overview of using a closure in a function definition, that gives you your own 'completionHandler' for functions in Swift 4. The end result I am talking about is something like this :
A completion handler is just a closure, and it is given the name 'completionHandler' in the function definition. A closure is a chunk of code that can be passed into a function, just like a property can, to be executed later in the course of that function.
Looking at the code snippet above, just after the 'completionHandler:' part of the call, I have a closure - thats the part in curly braces. The closure itself is passed into the function like a property in a way, along with all the other properties I might send into the function.
So how is that function defined, so that I can pass that closure in, call it a 'completionHandler', and get it to execute and a given time during that function call?
First, I need to know the kinds of properties I want to have returned from the call, for my closure code to use (the 'result, ref in' part). In my case, I wanted a true or false as to whether my network call had succeeded. Should it have worked, I wanted to know the database reference that the network call returned.
So I'm looking to get back a Bool property, and a Firebase type called DatabaseReference. As I say, this was just for my particular task - obviously you can return any amount of properties for your closure to use.
So heres the function definition itself to make that work :
Thats the whole thing. Breaking it down, the part that allows the closure to be passed in is this:
In a nutshell, I am later able to begin my closure with '{ result, ref in', because I have defined those types in the line of code above, as (Bool, DatabaseReference). The 'result' property is the Bool, and the 'ref' property is the DatabaseReference.
Just after that, I have '-> Void'. This just completes the definition of the type of closure I can send it. In this case, the closure is not expected to return anything to the method that calls it, such as 'return true'. Nothing is returned at all, ie - 'Void', so we dont have to 'return' in the closure statement.
Ignore the line of code with Database.database().. etc. Thats just the main task of my particular function in this case, which updated an online Firebase database. The important bit comes just after that, where we call the completionHandler closure to execute it.
To do that we simply call the 'name' of that closure as defined in the function - we gave it the name 'completionHandler' in the function definition. So we execute the closure by calling that named property, and we set the 'result' and 'ref' properties just after we call it. Like this :
And thats when the closure gets executed, with those properties that we wanted set. And thats it.
Theres more to closures of course, but this achieves most of what you need most of the time.
Subscribe to:
Post Comments (Atom)
Social Network with Firebase 3 - Infinitely scrolling UITableView of Firebase Items
Ive already talked about the basics of a Firebase social network project , and adding posts in two other articles. Here I want to explain ...
-
Ive already outlined my database structure for my Firebase based social network. Ive done the auth for new users , and Ive added each new ...
-
Im doing an awful lot of work at the moment with AVMutableVideoCompositions, and AVAssetExportSessions. When exporting a video using the la...
No comments:
Post a Comment