Good morning and welcome.
welcome to Asynchronous Design Patterns with Blocks, GCD, and XPC My name is Kevin Van Vechten and I'm the manager of the GCD and XPC teams.
And today we're going to start off with a bit of an overview of blocks and GCD and XPC for those of you who might not be familiar with the technology and then we're going to dive into some detail about what's new with Objective C and ARC and GCD and XPC and then finish up the talk with a lot of discussion about how to use these technologies to implement asynchronous design patterns in your code.
So first let's get started with an introduction to Blocks.
The best way to think about blocks by first thinking about function pointers in C. Blocks are really anonymous functions in C.
So here we have a type declaration of a callback function. It takes a string as an argument. It doesn't return any value and we can turn this into a block type simply by changing the star to a caret. (* to ^).
Caret is a special character that the compiler recognizes to indicate that this is a block type but otherwise it looks the same as a function pointer.
So we can use these blocks in a much different way than we typically use function pointers and we can actually declare a block variable so we have our type callback_block. The variable name is b.
And then instead of just giving it the address of a function to call we can actually give it the body of the function right there inline in our code. Declare the arguments, which in this case is a string, and then the body of code prints that string out.
Calling the block is really the same as invoking a function pointer in C. All you need to do is reference the block variable by name and pass it the arguments.
So in this example, if we were to compile and run this code, it would just print "hello world".
So declaring a body of code in place right where you need it is really the key benefit of blocks. The function pointers you need to define a function that's a helper somewhere else in your code you need to make changes there and wherever you use it.
You know, you need to renegotiate how you pass data to that code. But with Blocks since you declared them directly inline they actually can make reference to variables in the enclosing scope.
So if you have a big function and you declare a block in it any variables outside the block in the function are actually visible in the block and we'll go into detail in a moment to see how that works.
And even further than just referring to these variables, you can actually modify variables in the enclosing scope which is also very powerful.
So let's take a look at these three benefits of blocks in the contexts of three very common types of blocks.