Friday, December 17, 2010

Challenges of Creating the First iPhone App

Five months ago, I took the plunge and decided to take my work to the iPhone. My intention was to create apps that would be displayed on iPads. My vision was to have a gallery of apps on the iPad.

It took me a while to get this project done due to technical, transitional, financial challenges. It also took a long time because I wrote the App only during my spare time. As programming code is only a hobby and not a fulltime job(I'm a Cisco Network Engineer by day), I can only sneak in a few snippets of code every day or two. There were times when I didn't touch the code for weeks as I simply did not have time. I'd rather spend that time playing with my baby than code.

So here are a few challenges I encountered:
  • Financial Challenges
The ultimate goal of this work was to get these apps to the iPad. At the time when I first started writing the app, I only had an iPhone. Writing an app for the iPad would mean I would need to plonk the money for an iPad. This wasn't too much of a problem. My wife wanted an iPad so I got it for her with the intention of using her iPad for development.

What I didn't know was that for development, that means I would need to home her iPad to my Mac instead of it being homed to her PC. This provided challenges because that means I'll have to wipe her apps each time I want to develop on the iPad and it also means she'll need to wipe my code and apps whenever she wants to use her apps. It's quite a mess.

We both decided that we'll need 2 iPads. One for her and one for me. I needed one for development so eventually I had to wait until I can justify the purchase of another iPad.

  • Transitional Challenges
The next challenge had to do with porting my Java Processing code to Objective C code. I wrote C/C++ code 13 years ago and I barely remember how to code with C/C++ and OpenGL. I had to read a couple of books to learn how to use Objective C.

The challenge was porting my Java code to C++ code. My approach was a straight conversion of the Java classes to Objective C classes. This was a straight translation of code without any change in the way the algorithm is implemented.

Upon completion of the translation, I soon found out that I had a lot of typos in the equations and algorithms which caused a lot of problems during the translation. It was difficult tracking down where the errors in the calculation were occurring as there was a lot of code to eyeball. Eventually I found all of them and got them fixed.

One of the major differences between Java and Objective C is garbage collection. I take this for granted when coding with Java. Objective C has no automatic garbage collection so your memory management skills have to be on point. If you don't manage memory properly, your app will crash a lot. Relearning this skill wasn't too difficult but it sure made me learn how to be a better coder.

  • Technical Challenges
When the code was finally transitioned, I soon found out that a straight translation will not work. For one thing, the coordinate system for Java and OpenGL are different. Java's default coordinate system is the actual pixel dimension of the window. OpenGL has a scale range of -1.0 to 1.0. This means I had to re-tweak the code to accomodate the differences. Later on, I found out I can rewrite the OpenGL code to mimic the coordinate system of Java.

The next challenge is that I needed to optimize my code to the maximum. I've never coded for a system with limited resources before. I'm used to coding on powerful systems like Macbook Pros and never had to optimize my algorithms that much.

Coding for an iDevice is different. You have limited CPUs and memory to play with. I had to take my original code apart and optimize it. I had to re-write chunks of code so there are fewer loops and fewer function calls. I also had to re-write some functions in straight C to make it faster.

Objective C is a dynamically linked language. Although this is a feature of a language, it is also one of the things that slows it down. For each function call, the system has to allocate a new memory pointer. So for a huge for-loop, each call to a function takes a lot of resource and time to allocate the memory space.

Optimization was where I spent most of my time. Through function tweaks, compiler tweaks, and reconfiguring the code to have OpenGL use the GPU more, I was able to make the app faster than it's original form. I'm still not as happy as I want but it's acceptable right now.

---

The app is currently under review from Apple so it should be launched soon.

1 Comments:

Anonymous Jeff Clark said...

Thanks for the pointers Jim. I'm thinking about moving from Processing to iOS development as well sometime next year and you likely saved me from a few headaches.

7:08 AM  

Post a Comment

Links to this post:

Create a Link

<< Home