RWDevCon 2016 – Summary

On March 10 and 11 of 2016, I attended http://rwdevcon.com conference at Alexandria Virginia. This is a tutorial conference where the speakers give us starter projects and do live coding while explaining the concepts.

Below is the list of the sessions I attended and the summary of my learnings

  • Design Patterns in Swift
    1. Prefer composition over inheritance. Use other objects instead of inheriting behavior.
    2. Limit how much classes know about their dependencies. Prefer polymorphism over concrete classes
    3. Structural patterns: define how objects are composed. Eg: MVVM, MVC, FAcade
    4. Behavioral patterns:  define how objects communicate with each other : Eg: Strategy, Delegation
    5. Creational patterns: instantiate or creates objects for you
    6. When deciding to use Singleton, try for other alternatives. Make sure you are not using a singleton to pass state around.
    7. https://github.com/ochococo/Design-Patterns-In-Swift – great resource for design patterns examples in Swift
  • Advanced Debugging
    1. Cmd + K to empty the debug console
    2. Command to list all view controllers in the project that have viewDidLoad   ( image lookup –rn viewDidLoad <projectname>)
    3. In order to set breakpoints only on the  Objc version of a method syntax of breakpoints. To set breakpoint on setTitle of UIViewController b –[UIViewController setTitle:]
    4. How to attach debugger to any app and play around with the code and assembly registers
    5. https://github.com/facebook/chisel – lldb extensions used by facebook to debug iOS apps ( I am yet to look around)
    6. I will need to rewatch the video for this advanced session as I feel I havent completely grasped  a lot of the goodness presented.
  • Custom Controls
    1. If we want to create a custom controls that can be reused across app, “Add a New file” and select iOS\Source\CocoaTouch class ‘
    2. Override the init (frame: ) and init(coder aDecoder: NSCoder) to call super and prepareView() method ( where the necessary views are initialized, added to super view and constraints are defined)
    3. In iOS 9, we have NSAutoLayourAnchor. framework class introduced, that makes creating constraints a lot simpler in code
    4. Will present the changes necessary to ensure this custom view class to be used in the Interface Builder
    5. Sam Davies taught some very fancy animations that ability to manipulate them from outside custom control.
    6. 3 part Core Graphics tutorial resource for follow up: https://www.raywenderlich.com/90690/modern-core-graphics-with-swift-part-1
  • App Architecture
    1. How to create an Asynchronous NSOperation
    2. Use Dependency injection (with Swinject in the tutorial) to be able to easily swap underlying functionalities
    3. Recommended to use a “FlowController”  instead of Segues which makes changing the flow of the app simple to change in future.
    4. WWDC 2015 session 226: Advanced NSOperations
  • MVVM in practice
    1. MVVM is considered as an alternative design pattern to counter MVC ( Massive View Controller) . Here we separate out the model ( that converts data into UX – consumable  format into separate class)
    2. Explained how to create listener between model and the view controller using a generic object Box<T> ( Boxing concept where an object and its listeners are maintained in a single class and any changes in object are reported out to the listeners)
  • Xcode UI testing
    1. We can add a “iOS UI Testing Bundle” from “Add New File” Xcode menu that will create XCUI Test templates
    2. We can use XCUITesting recording behavior in order to record the actions we peform on the app
    3. We need to use XCTAssert*() methods when validating your test conditions
    4. In the app, we have “accessibility identifiers” and “accessibility labels” that we can set for every UI element. The labels are used when we turn on narrator. But the accessibility identifiers are used for the sole purpose of automation and hence set it to any value
    5. It is recommended to centralize the AccessibilityIdentifiers into a single enum
    6. WWDC 2015 Session 406
  • Core Data and Online service synchronization ( I unfortunately had to leave in between to catch an early flight – my poor decision – so I am looking forward to the video)
    1. This talk dealt with how to synchronize local data with internet backend
    2. Here instead of View controllers maintain both the local and remote service information, the recommendation is to have the local service wrap the remote service calls (similar to Opal’s personalization manager ) .
    3. The recommendation to synchronization is to get all local changes and send them to the server. Have the server return back the entire current state. The local layer then does a diff of server’s response with its current state and then decide if local data wins or server data wins
    4. It is explicitly recommended not to use a timestamp as a synchronization point between server and local, but instead use a pre-determined version number that the server will send back when sending the data
    5. Need to be careful to test the different scenarios:
      1. Conflicts
      2. Missing entities ( local or remote)
      3. Connection failure
  • Other sessions that I very much looking forward to watch the videos are:
    • Protocol Oriented Programming.
    • Advanced Autolayout
    • Beginning UIStackView
    • Beginning App Search

It has been  a very well organized, well thought out conference and speeches where we could see the speakers were experts in thier areas and very well prepared. I really enjoyed the conference and will definitely be attending the next one.

PS: If interested this is a conference post-mortem post by the main organizer https://www.raywenderlich.com/128351/rwdevcon-2016-post-mortem

I have blogged about my personal experience here http://swiftgazelle.com/2016/03/rwdevcon-2016-my-thoughts/

Screen Shot 2016-03-16 at 4.42.29 PM

RWDevCon 2016 – Summary

RWDevCon 2016 – My thoughts

This post is to summarize my personal experience with one of the most amazing conferences I have ever been to
The awesome team behind the world-famous Ray Wenderlich tutorials have organized a unique iOS conference for the first time in 2015. It has been such a hit, that I had to buy conference ticket 4 months in advance to get in 2016. Among others, the most distinguishing aspect of this event is the “Hands on tutorials”. The awesome speakers at the Ray Wenderlich will not just preach ( like other conferences) but also make the attendees get their hands dirty with code. I want your to understand how big of a deal this is. As any good  software engineer knows, the only way to internalize any software concept is by actually coding it out. It took me years for me to come to terms that we need to implement (code) concepts without theory, just to understand them ( as opposed to the exams where you study, understand and apply concepts in exam, but I digress).

I am going to repeat the single most important fact of being a software engineer ( in blocks now)

As any good  software engineer knows, the only way to internalize any software concept is by actually coding it out

In my opinion, people attend conferences for learning important and most relevant concepts in their field of study. Thus for a software engineer, the best way to understand a new concept presented at conference is to code it out. As far as I am aware, RWDevCon is the only conference that has been able to incorporate coding into the teaching process and are extremely successful, while they are at it.

I have been working as an iOS developer for less than a year and I have gained a lot of knowledge and tons of additional research to do.

The keynote speech by the great Ray Wenderlich himself, inspiration talks by those who made big in the mobile development space, gave me some thoughtful reflection opportunities after a grueling coding sessions.

The RWDevCon platinum sponser, CouchDB, organized an amazing party after the first day of conference with open bar 0317

After which I got to play the board game , “Bohnanza”pic69366

with some of the most amazing, intelligent people, including couple of the speakers. I have made some very good friends out of the 2 hours negotiation-roller coaster of Bohnanza game (which I highly recommend you try). I think this board game is the single most brilliant ice-breaker for an introvert like me to network in conferences. I really made some good friends (https://twitter.com/zombietreats, https://twitter.com/matt_luedke) that I hope to foster.

Thanks to the amazing organizers, tech editors, speakers and Ray himself for deciding to have the conference in 2016 as well and helping me become a better iOS developer and a better person.

Below is the picture of our “board game team extraordinaire” who played for atleast couple of hours at party.

boardGame

PS: I will create another post with the summary of the conference tutorials I attended as this is more of my personal experience of the conference.

 

RWDevCon 2016 – My thoughts

Asynchronous NSOperation: Why and how?

why-so-asynchronous

Asynchronous is a usually a scary concept for beginners. When I initially started my foray into iOS development ( my first development experience actually), I was very much intimidated about the prospect of me having to do asynchronous programming. But as I started to familiarize myself with iOS concepts and Swift language, I have increasingly noticed Apple has made it very easy for iOS app developers to use Asynchronous programming.

There are 3 kinds of asynchronous scenarios that you would want to build in your app (AS1, AS2  == Asynchronous operation 1, Asynchronous operation 2)

  1. Fire AS1, Wait for AS1 to return, If AS1 succeeds only then start AS2 potentially using data from AS1.
  2. Fire AS1, Fire AS2 (The firing of AS2 is independent of return value of AS1)
  3. Fire AS1, Wait for AS1 to return, fire AS2 ( Here too, the firing of AS2 is independent of AS1, but sometimes we want to restrict the number of active asynchronous operations)

We have many amazing open source libraries that simplifies coding for scenario 1. Eg: http://reactivecocoa.io/

For scenario 2, we can use the following steps ( this can be achieved in different ways. Describing one way briefly here, I believe this can be another blog post in itself)

  • Create a NSOperationQueue with appropriate specifications
  • Create NSURLSession with the delegate set to the operation queue created above
  • dataTask1 = NSURLSession.dataTaskWithRequest(NSURLRequest for of the AS1 task). dataTask1.start()
  • dataTask2 = NSURLSession.dataTaskWithRequest(NSURLRequest for of the AS2 task). dataTask2.start()

In the above scenario dataTask2 is kicked off even before dataTask1 returns.

Scenario 3 is what I will be focussing on in my post.

The Apple’s framework class I will be using for this are below:

  1. NSOperationQueue
  2. NSOperation

NSOperation basics

I can’t go about explaining how to implement an Asynchronous NSOperation without describing few basics about the built-in framework class NSOperation. Below are some of the important points we need to be aware of

  • NSOperation is an abstract class that provides a way to represent the data and the logic associated with a single task ( The task can be as simple/complex as you like)
  • It contains a lot of built-in logic that “coordinates the safe execution of your task” – as the Apple documentation puts it
  • So we can derive from NSOperation class, put the logic of the task by overriding main() of NSOperation, create an operation object and class start() to execute the code.
  • NSoperation keeps track of the state of the task. A task at any given time can be in 3 different states.
    • Ready
    • Executing
    • Finished ( Completed/Cancelled)
  • Below image is my attempt to create a flowchart of how the NSOperation works.

Screen Shot 2016-03-06 at 4.04.53 PM

Why doesnt this work for Asynchronous task logic?

The state tracking mechanism of nsoperation that I have described above works with the assumption that the custom task logic is complete as soon as the execution is out of main() function. In other words, the nsoperation is built under the assumption that the custom logic is NOT asynchronous.

Aysnchronous NSOperation

The NSOperation documentation guides us on what NSoperation functions/properties needs to be overriden if our task logic is asynchronous:

  • asynchronous (override to return true)
  • executing
  • finished
  • start

What this suggests is that we need manually keep track of the state of our subclass of NSOperation. We also need to ensure we are firing all the necessary KVO ( Key Value Observation) events associated with the state change. the Ray Wenderlich tutorial on “Asynchronous Operations” introduced me to the

Below is the code for the state and overriding of the suggested properties and methods

In the above code we create a custom variable state ( of the custom enum State type). This is initially set to .Ready as we use the in-built method calls willChangeKeyValue and the didChangekeyValue to fire the necessary KVO events whose utility we will explore shortly.

In the beginning of both the start() and main() method we check if the operation has been cancelled and prevent any further execution. In order to begin execution of operation, create an instance of AsynchronousOperation and call start() on it. This operation’s status will not be set to .Finished until the asynchronous operation completes and executes the callback block as specified in the code above.

Let’s re-write scenario 3 once again:

Fire asynchronous operation 1, Wait for asynchronous operation1 to return, fire asynchronous operation2

For achieving this, we need an “NSOperationQueue” which, as the name suggests, queues up NSOperation objects and schedules them depending on the queue specifications and the status of the objects. This NSOperationQueue makes use of the KVO of the operation state in determining the operation state and scheduling the next one in the queue.

In our case, we need only one operation to be scheduled at a time and the second one to be scheduled only of the first one finishes ( either successful or failed or cancelled).  So, when setting up the NSOperationQueue we set its maxConcurrentOperationCount to be 1 as below:

Due to the state management and KVO we have authored, we are guaranteed that the asyncOperation2 is executed only when the first operation is finished.

Summary

We have created a custom subclass of NSOperation to ensure that we can track an asynchronous operation status utilizing the existing NSOperation state concepts. This NSOperation is then scheduled on an NSOperationQueue with maxConcurrentOperationCount = 1, to ensure that we have only one active asynchronous operation at anytime.

Image