<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Nicolás Miari</title>
    <description>I prefer to drink Java and code in Cocoa than the other way around.
</description>
    <link>http://nicolasmiari.com/</link>
    <atom:link href="http://nicolasmiari.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 08 Aug 2020 06:18:37 +0000</pubDate>
    <lastBuildDate>Sat, 08 Aug 2020 06:18:37 +0000</lastBuildDate>
    <generator>Jekyll v3.9.0</generator>
    
      <item>
        <title>Developing a Game Engine Part III: Stage Structure</title>
        <description>&lt;p&gt;(Read Part 2 &lt;a href=&quot;http://nicolasmiari.com/development/2017/08/17/GameEngine_2.html&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;h3 id=&quot;whats-in-a-stage&quot;&gt;What’s in a Stage&lt;/h3&gt;

&lt;p&gt;So far we’ve talked about how stages are linked together and how to get from one
to another. However, we still don’t know what the actual stages look like 
internally; let’s think about it now.&lt;/p&gt;

&lt;h4 id=&quot;substructure&quot;&gt;Substructure&lt;/h4&gt;

&lt;p&gt;At first glance, it feels as if each stage is just a tile map with objects in 
it. These include collectible objects (gems, coins, inventory items, etc.), 
traps, triggers (including exits), NPCs, etc. each placed at a specific 
location of the map grid. But we can do better than this.&lt;/p&gt;

&lt;p&gt;In some 2D platformers, action within a stage isn’t just continuous scrolling 
along a large map: sometimes the player goes through a “door” that leads to a
different “room”, typically immediately. Sort of like linking between stages, 
but within the same stage, and perhaps you can traverse the door back in the 
direction you came from (but not always: think trapdoors). The goal is to make
the stage experience richer by providing different sub-environments, perhaps 
each with its own mood: a unique visual style, or eve a different BGM 
altogether.&lt;/p&gt;

&lt;p&gt;So, a stage is (again), a (possibly directed) network of tile maps. This 
generalizes the notion of a &lt;strong&gt;gate&lt;/strong&gt; into a trigger that leads to a specific 
location on another map, possibly in a different stage.&lt;/p&gt;

&lt;h4 id=&quot;contents&quot;&gt;Contents&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Coming next: Part 4.&lt;/em&gt;&lt;/p&gt;

</description>
        <pubDate>Fri, 18 Aug 2017 01:53:00 +0000</pubDate>
        <link>http://nicolasmiari.com/development/2017/08/18/GameEngine_4.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/development/2017/08/18/GameEngine_4.html</guid>
        
        
        <category>development</category>
        
      </item>
    
      <item>
        <title>Developing a Game Engine Part II: Stage Architecture</title>
        <description>&lt;p&gt;(Read Part 1 &lt;a href=&quot;http://nicolasmiari.com/development/2017/08/16/GameEngine_1.html&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;h3 id=&quot;stages&quot;&gt;Stages&lt;/h3&gt;

&lt;p&gt;So I set to develop a game engine so generic that, just by providing custom 
graphic and sound assets, and tweaking a few settings here and there, it could 
produce the working code for any game I want.&lt;/p&gt;

&lt;p&gt;This means that I need to isolate the most general structure shared by all the 
games I expect to develop (i.e., 2D action/puzzle platformers), and work my way 
from there.&lt;/p&gt;

&lt;p&gt;Let’s take a moment and think what that is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;, a basic 2D platformer consists of a &lt;em&gt;series&lt;/em&gt; of stages. You start at 
stage 1 and proceed until you find the exit, which leads to stage 2. Once you 
complete all stages, you have finished the game.&lt;/p&gt;

&lt;p&gt;But is that the most &lt;em&gt;general&lt;/em&gt; structure possible? Definitely not: some games 
have a “world map” connecting several “village” stages. With exceptions 
-typically dictated by game progress- you can get to any stage from the world 
map. Once you enter a stage, some games let you “return to 
map” at any moment (typically from the “pause” menu), while others force you to 
complete the current stage first. And even if the game is “linear” 
as in the previous paragraph, the condition to complete a stage can be anything 
from finding an exit, to defeating a boss, or collecting a certain amount of 
items.&lt;/p&gt;

&lt;p&gt;So, a game is not a series of stages: it is more of a (potentially directed) 
&lt;strong&gt;network&lt;/strong&gt;. In games that include a “world map”, the map itself is also a stage 
(with very different rules from the “village” stages). There’s even the 
possibility that there are multiple world maps, linked by (for example) bridges 
that become accessible partway through the game, etc.&lt;/p&gt;

&lt;h4 id=&quot;gates&quot;&gt;Gates&lt;/h4&gt;

&lt;p&gt;Each stage can be entered and exited somehow. For this, we need portals or 
gates. A gate is an object placed somewhere within a stage. It can work as 
either an entrance or an exit. When an exit is entered by the player character, 
it transports it to a specific entrance of another stage! That is, each exit 
gate leads to an entrance gate in some stage (entrance stages do nothing when 
entered: they are mere “starting points” for game play on that stage).&lt;/p&gt;

&lt;p&gt;Each stage can have more than one exit: think of the world map we described 
earlier. But it can also have more than one entrance: Each particular “village”
stage, when completed, takes the player character back to the world map, at the 
location of the village’s entrance!&lt;/p&gt;

&lt;h4 id=&quot;cut-scenes&quot;&gt;Cut Scenes&lt;/h4&gt;

&lt;p&gt;We can generalize the concept of the stage further: What about the “cut scenes”?
You know, those animations that typically happen in between (sometimes within) 
stages, to advance the story? Those can be though of as special cases of stages 
too. The inter-stage cut scenes are just stages where there is no user input and
all the characters are scripted! Of course, scripting and temporary cancellation 
of user input can also happen temporarily within a ‘regular’ stage, perhaps 
triggered by some action of one of the characters (e.g., stepping on a trap).&lt;/p&gt;

&lt;h4 id=&quot;triggers&quot;&gt;Triggers&lt;/h4&gt;

&lt;p&gt;This bring us to the concept of triggers, which are nothing more than a 
generalization of the exit gates we talked about earlier. A trigger is basically 
a location on a stage (a single tile or a group af adjascent tiles, if it needs 
to be larger) that, when entered, causes some predetermined action to happen. 
The action can be:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Teleport the player character (and thus, advance the game) to a specific 
location (entrance) of a specific stage,&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Run a script that causes change and/or programmed motion in one or more 
characters or map objects (optionally, disabling user input while at it)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;etc.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Coming next: Part 3.&lt;/em&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 17 Aug 2017 01:53:00 +0000</pubDate>
        <link>http://nicolasmiari.com/development/2017/08/17/GameEngine_3.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/development/2017/08/17/GameEngine_3.html</guid>
        
        
        <category>development</category>
        
      </item>
    
      <item>
        <title>Developing a Game Engine Part II: Stage Architecture</title>
        <description>&lt;p&gt;(Read Part 1 &lt;a href=&quot;http://nicolasmiari.com/development/2017/08/16/GameEngine_1.html&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;h3 id=&quot;stages&quot;&gt;Stages&lt;/h3&gt;

&lt;p&gt;So I set to develop a game engine so generic that, just by providing custom 
graphic and sound assets, and tweaking a few settings here and there, it could 
produce the working code for any game I want.&lt;/p&gt;

&lt;p&gt;This means that I need to isolate the most general structure shared by all the 
games I expect to develop (i.e., 2D action/puzzle platformers), and work my way 
from there.&lt;/p&gt;

&lt;p&gt;Let’s take a moment and think what that is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;, a basic 2D platformer consists of a &lt;em&gt;series&lt;/em&gt; of stages. You start at 
stage 1 and proceed until you find the exit, which leads to stage 2. Once you 
complete all stages, you have finished the game.&lt;/p&gt;

&lt;p&gt;But is that the most &lt;em&gt;general&lt;/em&gt; structure possible? Definitely not: some games 
have a “world map” connecting several “village” stages. With exceptions 
-typically dictated by game progress- you can get to any stage from the world 
map. Once you enter a stage, some games let you “return to 
map” at any moment (typically from the “pause” menu), while others force you to 
complete the current stage first. And even if the game is “linear” 
as in the previous paragraph, the condition to complete a stage can be anything 
from finding an exit, to defeating a boss, or collecting a certain amount of 
items.&lt;/p&gt;

&lt;p&gt;So, a game is not a series of stages: it is more of a (potentially directed) 
&lt;strong&gt;network&lt;/strong&gt;. In games that include a “world map”, the map itself is also a stage 
(with very different rules from the “village” stages). There’s even the 
possibility that there are multiple world maps, linked by (for example) bridges 
that become accessible partway through the game, etc.&lt;/p&gt;

&lt;h4 id=&quot;gates&quot;&gt;Gates&lt;/h4&gt;

&lt;p&gt;Each stage can be entered and exited somehow. For this, we need portals or 
gates. A gate is an object placed somewhere within a stage. It can work as 
either an entrance or an exit. When an exit is entered by the player character, 
it transports it to a specific entrance of another stage! That is, each exit 
gate leads to an entrance gate in some stage (entrance stages do nothing when 
entered: they are mere “starting points” for game play on that stage).&lt;/p&gt;

&lt;p&gt;Each stage can have more than one exit: think of the world map we described 
earlier. But it can also have more than one entrance: Each particular “village”
stage, when completed, takes the player character back to the world map, at the 
location of the village’s entrance!&lt;/p&gt;

&lt;h4 id=&quot;cut-scenes&quot;&gt;Cut Scenes&lt;/h4&gt;

&lt;p&gt;We can generalize the concept of the stage further: What about the “cut scenes”?
You know, those animations that typically happen in between (sometimes within) 
stages, to advance the story? Those can be though of as special cases of stages 
too. The inter-stage cut scenes are just stages where there is no user input and
all the characters are scripted! Of course, scripting and temporary cancellation 
of user input can also happen temporarily within a ‘regular’ stage, perhaps 
triggered by some action of one of the characters (e.g., stepping on a trap).&lt;/p&gt;

&lt;h4 id=&quot;triggers&quot;&gt;Triggers&lt;/h4&gt;

&lt;p&gt;This bring us to the concept of triggers, which are nothing more than a 
generalization of the exit gates we talked about earlier. A trigger is basically 
a location on a stage (a single tile or a group af adjascent tiles, if it needs 
to be larger) that, when entered, causes some predetermined action to happen. 
The action can be:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Teleport the player character (and thus, advance the game) to a specific 
location (entrance) of a specific stage,&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Run a script that causes change and/or programmed motion in one or more 
characters or map objects (optionally, disabling user input while at it)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;etc.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Coming next: Part 3.&lt;/em&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 17 Aug 2017 01:53:00 +0000</pubDate>
        <link>http://nicolasmiari.com/development/2017/08/17/GameEngine_2.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/development/2017/08/17/GameEngine_2.html</guid>
        
        
        <category>development</category>
        
      </item>
    
      <item>
        <title>Developing a Game Engine, Part I: The Genesis</title>
        <description>&lt;h3 id=&quot;background&quot;&gt;Background&lt;/h3&gt;

&lt;p&gt;In 2011 I was working at a small shop that specializes in content creation and 
manangement of their own IP assets. At some point I was given an incredible 
amount of creative freedom and set to develop my own idea of a 2D game.&lt;/p&gt;

&lt;p&gt;I had my own sprite library I had been polishing since 2010 and soon found out 
that I also needed I tilemap editor (I didn’t want to use an existing tool: in line
with my ideology of creating my own sprite library from scratch, I wanted a custom
tilemap engine that was compatible with it).&lt;/p&gt;

&lt;p&gt;It worked but it was buggy and feature limitted; I could live with it for the 
short period of time it took to develop the nine stages of the very simple game, 
but it was too much to ask other people to use it (or even myself, in the long 
run).&lt;/p&gt;

&lt;p&gt;The game didn’t do well, but after I left the job the good folks at the company 
ceded the rights to the app to me (it was my brainchild after all: not just the 
code but the whole idea and visual design too. Also, it wasn’t IP good enough 
that they could monetize. I’m not the guy who invented Pokémon or anything). In 
the end, the custom sound library I was using broke with one of the iOS updates 
(it seemed to rely on a bug that got fixed, apparently!) and I removed it from 
sale.&lt;/p&gt;

&lt;p&gt;I promised to myself that I would recreate the map editor from scratch, 
one day, update my game with support for modern devices, re-release it, and move
on to develop further games.&lt;/p&gt;

&lt;p&gt;I have been trying since, but at each turn I found myself facing design 
that I could not solve. I wanted my map editor to have the capability of 
“testing” and tuning each map in real time, during development (without having 
to export it and import into the game project each time), but on the other hand 
I didn’t want the editor to know too much about the game in order to keep it 
generic and versatile.&lt;/p&gt;

&lt;h3 id=&quot;the-turning-point&quot;&gt;The Turning Point&lt;/h3&gt;

&lt;p&gt;Last night I couldn’t sleep and set to think about what I’m rally trying to do
with my (stagnant) personal project. I decided to change my mentality and 
develop a full-fledged game editor.&lt;/p&gt;

&lt;p&gt;A game-agnostic map editor that can also “test run” the maps being created is an
impossibility. Instead, I will draw some very generic constraints of what 
constitutes a tilemap-based game, and design my game editor around that. It will 
be basically a game template with lots of options to configure.&lt;/p&gt;

&lt;p&gt;The bonus is that most of the logic will be coded once, into the engine 
framework, and each particular game I develop after that will consist of custom 
assets and settings of the engine. Kind of like Unity3d or Unreal Engine, but 
tailored to my own goals.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next: &lt;a href=&quot;http://nicolasmiari.com/development/2017/08/17/GameEngine_2.html&quot;&gt;Part 2&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
        <pubDate>Wed, 16 Aug 2017 01:53:00 +0000</pubDate>
        <link>http://nicolasmiari.com/development/2017/08/16/GameEngine_1.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/development/2017/08/16/GameEngine_1.html</guid>
        
        
        <category>development</category>
        
      </item>
    
      <item>
        <title>DinnerJacket.framework</title>
        <description>&lt;p&gt;&lt;img src=&quot;/assets/images/FrameworkIcon.png&quot; alt=&quot;Frameworks Icon&quot; class=&quot;center-image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There’s recently a trend to give open source libraries and frameworks &lt;strong&gt;fancy names&lt;/strong&gt; that have nothing to do with the software’s functionality; for example: &lt;a href=&quot;https://github.com/Alamofire/Alamofire&quot;&gt;Alamofire&lt;/a&gt;, &lt;a href=&quot;https://github.com/bignerdranch/Freddy&quot;&gt;Freddy&lt;/a&gt;&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, and &lt;a href=&quot;https://github.com/daltoniam/starscream&quot;&gt;Starscream&lt;/a&gt; (to name a few ones that are prominent in the “cocoasphere”).&lt;/p&gt;

&lt;p&gt;I started developing my very own &lt;strong&gt;sprite library&lt;/strong&gt; for iOS back in 2010, and although I do not work on it very often, I keep improving it and over the years and it has gone through several incarnations (and names).&lt;/p&gt;

&lt;p&gt;Back then, I had just finished my first iPhone game &lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;, using the (then popular) open source library known as &lt;a href=&quot;http://cocos2d-objc.org&quot;&gt;Cocos2d-iPhone&lt;/a&gt;.
The game was a simple 3D puzzler, but the 3D requirements were so minimal that I just needed to write some custom OpenGL ES 1.1 code on top of the library’s existing functionality (unlike SpriteKit, cocos-2d iPhone provides direct access to the OpenGL ES layer for performing custom drawing other than 2D sprites).&lt;/p&gt;

&lt;p&gt;Cocos2d is a great library and very easy to use; but there were a few things that bugged me about it. At that moment, Apple and other smartphone manufacturers were transitioning to GPUs with a programmable pipeline (i.e., shaders), but the current version of Cocos2d was stuck in the older, fixed-pipeline version of OpenGL ES (1.1).
Although the geometry of my game was minimal, I still wanted to be able to experiment with fancy visual effects in order to
enhance the player’s experience. The programmable pipeline-ready 2.0 API was there for anyone to use, but the people developing Cocos2d just hadn’t had time to make the transition yet.&lt;/p&gt;

&lt;p&gt;Secondly, even though I am a computer graphics major, back then the last graphics programming I had done was learning some Direct Draw at a previous job, and before that, immediate-mode desktop OpenGL in the early 00’s at college. In the constantly evolving mobile arena of modern times, I really felt left behind. I needed to learn the present-day graphics programming idioms and techniques.&lt;/p&gt;

&lt;p&gt;Finally, it was &lt;strong&gt;somebody else’s code&lt;/strong&gt;. I am not an advocate of reinventing the wheel every time (especially when the available options are so plenty and so good), but there are exceptions.&lt;/p&gt;

&lt;p&gt;In this particular case, I felt I would rather be someone with enough technical skills to &lt;strong&gt;build his own sprite library from scratch&lt;/strong&gt;, than just another programmer who knows how to &lt;strong&gt;use&lt;/strong&gt; somebody else’s library.&lt;/p&gt;

&lt;p&gt;Fast forward to today, and iOS has made great strides. OpenGL ES, as of version 3.0, has converged closer towards its desktop counterpart. And regarding my “library”, what used to be a folder with source files copied and pasted from one project to another (and all the version control madness that entails), is now a dynamic framework whose source code I can keep in one place.&lt;/p&gt;

&lt;p&gt;Following the trend mentioned at the beginning of the article, I have re-christened the code base to &lt;strong&gt;&lt;em&gt;Dinner Jacket&lt;/em&gt;&lt;/strong&gt;. The name merely reflects something that I like (evening dress), and has absolutely nothing to do with the library’s capabilities.&lt;/p&gt;

&lt;p&gt;Although the days of OpenGL (and Objective-C) seem to be numbered, I owe to my library a big, final update to modern standards before I move on to other projects. Also, there is a small trail of games I developed mostly in my spare time, over the years, that broke on way or another on subsequent OS updates and I was forced to pull from the AppStore. I owe those games a remake and a relaunch.&lt;/p&gt;

&lt;p&gt;But after that is done, I will port the framework to the Metal API, and at the same time rewrite it in Swift.&lt;/p&gt;

&lt;h4 id=&quot;update-2017-oct-11&quot;&gt;&lt;em&gt;Update (2017 Oct 11)&lt;/em&gt;&lt;/h4&gt;
&lt;p&gt;I have officially abandoned development of DinnerJacket and moved to my next project library, redesigned from scratch with Swift and Metal in mind (some design ideas were inherited, though). The new project is still private, but I will be writing blog posts on its design and architexture.&lt;/p&gt;

&lt;p&gt;In the mean time, DinnerJacket is now &lt;strong&gt;open source&lt;/strong&gt; and the (first and) final release is available on &lt;a href=&quot;https://github.com/nicolas-miari/DinnerJacket&quot;&gt;GitHub&lt;/a&gt;, for reference and demonstration purposes.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;To be fair, this one is sort of a pun. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;As is usually the case with someone who can not make time to keep his personal projects up to date, it is currently removed from sale until I can modernize it. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Tue, 11 Oct 2016 10:33:00 +0000</pubDate>
        <link>http://nicolasmiari.com/development/2016/10/11/DinnerJacket.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/development/2016/10/11/DinnerJacket.html</guid>
        
        
        <category>development</category>
        
      </item>
    
      <item>
        <title>Icon Set Creator</title>
        <description>&lt;p&gt;&lt;img src=&quot;/assets/images/IconSetCreator-512.png&quot; alt=&quot;App Icon&quot; class=&quot;center-image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Thank you to the 14k+ people who downloaded &lt;a href=&quot;https://itunes.apple.com/us/app/icon-set-creator/id939343785?mt=12&quot;&gt;Icon Set Creator&lt;/a&gt; since its initial 
release on January, 2015!&lt;/p&gt;

&lt;p&gt;I have just submitted the latest version (1.1.2) to the Mac App Store for review.
New features include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Bug fixes&lt;/li&gt;
  &lt;li&gt;iPad Pro App Icon support&lt;/li&gt;
  &lt;li&gt;Dark mode!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay tuned for the imminent release.&lt;/p&gt;

</description>
        <pubDate>Tue, 15 Mar 2016 08:00:00 +0000</pubDate>
        <link>http://nicolasmiari.com/apps/2016/03/15/Icon-Set-Creator.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/apps/2016/03/15/Icon-Set-Creator.html</guid>
        
        
        <category>apps</category>
        
      </item>
    
      <item>
        <title>Serializing Asynchronous Operations Using Closures and Private Functions</title>
        <description>&lt;p&gt;Sometimes you need to perform an asynchronous task, like downloading a file. In 
swift, closures come in very handy to perform some operation once the asynchronous
task completes. For example:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;func downloadFile(fromURL url:NSURL, completion:((error:NSError?)-&amp;gt;()))
{
    MyHTTPLibrary.sendHTTPRequest({ response in
    
        if response.statusCode == 200 {
            // SUCCESS
            completion(error:nil)
        }
        else{
            // FAILURE:
            completion(error: response.error!)
        }
    })
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And some &lt;em&gt;other&lt;/em&gt; times, you need to perform &lt;em&gt;a lot&lt;/em&gt; of asynchronous tasks (like&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;for example- downloading a lot of files). We could expand the previous code
with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop, like this:&lt;/p&gt;

    &lt;p&gt;func downloadFiles(fromURLs urls:[NSURL], completion:((error:NSError?)-&amp;gt;()))
  {
      for url in urls {
          self.downloadFile(fromURL:url, completion:{ error in 
              if error != nil {
                  completion(error:error)
                  break
              }&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;          if url == urls.lastObject(){
              completion(error:nil)
          }
      })
  }   }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will loop through all the URLs and send an asynchronous request for each. The problem is, all the requests are
sent almost at the same time: Most likely, you will finish sending the last request &lt;em&gt;before&lt;/em&gt; you get the first response.
Moreover, we have setup our success trigger (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;completion(error:nil)&lt;/code&gt;) at the end of the completion handler of the last request. This assumes that the last request will complete last: a race condition.&lt;/p&gt;

&lt;p&gt;A better design requires that we serialize (or enqueue) all our requests, so that they are performed one at a time.
This way, no matter how long each request takes, we can be sure that request number N+1 is sent only &lt;em&gt;after&lt;/em&gt; we received
a response to request number N. Also, we don’t need to worry about our HTTP library having to deal with too many 
concurrent connections.&lt;/p&gt;

&lt;p&gt;In a delegate-based scenario, we would have a mutable array of connection tasks serving as a queue, and dequeue them
one by one on the delegate method that is called on completion, to start the new task.&lt;/p&gt;

&lt;p&gt;But with colsures, we have all our code on-site (no separate methods), so at first it is not clear how to “serialize”
all our downloads. However, with a clever trick we can accomplish this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;func downloadFiles(fromURLs urls:[NSURL], completion:((error:NSError?)-&amp;gt;()))
{
    // [1] Define recursion:

    var mutableURLs = urls

    func downloadNext() 
    {
        if mutableURLs.count == 0 {
            // Terminating condition
            return completion(error:nil)
        }

        let url = urls.removeFirst()

        self.downloadFile(fromURL:url, completion:{error in 
            guard error == nil else {
                // Failure; abort:
                completion(error:error)
                return
            }

            // Success; recurse:
            donwloadNext()
        })
    }


    // [2] Kickstart recursion :

    donwloadNext()
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The code runs as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We make a mutable copy of the URL array.&lt;/li&gt;
  &lt;li&gt;Call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downloadNext()&lt;/code&gt; for the first time: the array is not empty. We remove first URL and send an asynchronous request to it.&lt;/li&gt;
  &lt;li&gt;Return from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downloadNext()&lt;/code&gt; right away.&lt;/li&gt;
  &lt;li&gt;Return from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downloadFiles()&lt;/code&gt; right away.&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
  &lt;li&gt;On request completion, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downloadNext()&lt;/code&gt; calls itself -this time, with the diminished URL array- and the
cycle goes on, until the array is empty and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downloadNext()&lt;/code&gt; bails out to success.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All in all, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;downloadNext()&lt;/code&gt; is called N+1 times to perform N requests (the last time finds the URL array emoty and bails out).&lt;/p&gt;
</description>
        <pubDate>Fri, 11 Mar 2016 11:38:00 +0000</pubDate>
        <link>http://nicolasmiari.com/personal/2016/03/11/Serializing-Asynchronous-Operations-With-Closures.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/personal/2016/03/11/Serializing-Asynchronous-Operations-With-Closures.html</guid>
        
        
        <category>personal</category>
        
      </item>
    
      <item>
        <title>Hello World!</title>
        <description>&lt;p&gt;This is my first post using Jekyll/markdown.&lt;/p&gt;

&lt;p&gt;This is a &lt;a href=&quot;http://stackoverflow.com/users/433373/nicolasmiari?tab=profile&quot;&gt;link&lt;/a&gt; to my Stack Overflow profile.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/FullSizeRenderSmall.jpeg&quot; alt=&quot;My helpful screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;(You can &lt;a href=&quot;/assets/images/FullSizeRenderSmall.jpeg&quot;&gt;download that image&lt;/a&gt; directly.)&lt;/p&gt;

</description>
        <pubDate>Tue, 01 Dec 2015 05:47:37 +0000</pubDate>
        <link>http://nicolasmiari.com/personal/2015/12/01/hello-world.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/personal/2015/12/01/hello-world.html</guid>
        
        
        <category>personal</category>
        
      </item>
    
      <item>
        <title>Welcome to Jekyll!</title>
        <description>&lt;p&gt;You’ll find this post in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jekyll serve&lt;/code&gt;, which launches a web server and auto-regenerates your site when a file is updated.&lt;/p&gt;

&lt;p&gt;To add new posts, simply add a file in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory that follows the convention &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YYYY-MM-DD-name-of-post.ext&lt;/code&gt; and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.&lt;/p&gt;

&lt;p&gt;Jekyll also offers powerful support for code snippets:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Tom'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; prints 'Hi, Tom' to STDOUT.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check out the &lt;a href=&quot;http://jekyllrb.com/docs/home&quot;&gt;Jekyll docs&lt;/a&gt; for more info on how to get the most out of Jekyll. File all bugs/feature requests at &lt;a href=&quot;https://github.com/jekyll/jekyll&quot;&gt;Jekyll’s GitHub repo&lt;/a&gt;. If you have questions, you can ask them on &lt;a href=&quot;https://talk.jekyllrb.com/&quot;&gt;Jekyll Talk&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Tue, 01 Dec 2015 05:47:37 +0000</pubDate>
        <link>http://nicolasmiari.com/jekyll/update/2015/12/01/welcome-to-jekyll.html</link>
        <guid isPermaLink="true">http://nicolasmiari.com/jekyll/update/2015/12/01/welcome-to-jekyll.html</guid>
        
        
        <category>jekyll</category>
        
        <category>update</category>
        
      </item>
    
  </channel>
</rss>
