iOS Development Journal

Tips and Tricks Learned the Hard Way

Alignment Options in Auto Layout

In OS X Lion and iOS 6 Apple added a new way to layout views, Auto Layout. This is a very powerful system that will handle a lot of the work and overhead of arranging your views. A well defined layout can make many headaches disapear entirely. However, it is a bit…quirky…to say the least.1

I recommend that you read the guide to the visual format language before going on.

This post will discuss the various options that you can set in constraintsWithVisualFormat:options:metrics:views:. These options affect how the views described in the format string are to be aligned with each other.

Running URL Requests in the Background

Getting data from the web is a pretty common operation for apps these days. Part of the marvel of the iPhone is that you’re always connected. Now, often we make network requests and don’t worry much about them until they’re done. What you might not know is where the code to handle the request is running. And by default that place often ends up being the main thread.

AFNetworking takes care of this issue for you. If you’re curious how it works, or can’t use AFNetworking then read on.

So if you’d like to learn more about how to handle NSURLConnections in the background, read more. If not, just go and use AFNetworking.

Forcing Things to Run on the Main Thread

The Problem

So Apple recommends that we avoid doing too much work on the main thread. This is great advice, the less time you spend wasting CPU cycles in the main thread, the more responsive your UI will be. In my opinion a well defined iOS application will avoid doing more than update the UI on the main thread.

One prime example I’ve run into was some code that did a synchronous network request. It waited for the network response before returning and someone thought it had to run on the main thread. This resulted in a lock-up during some animations I was working on.

There’s a number of topics1 that I plan to discuss about getting things off of the main thread, but today I’m going to discuss running things on the main thread.

An Introduction

The Beginning

This is one of those boring general posts designed to introduce you to this website, or “blog”. Rather than read this, I hope you’re just reading the articles as I publish them. If you’re really curious about me, and why I do this site, and where I’m getting the content, you can read on.