Purple Haze!

19 Jan, 2010

Making Countify & Data persistence

Posted by: Chris In: Programming

It was to be expected that I wouldnt be able to sit and learn iPhone development without interruption, so I’ve had to scale it back for a while whilst fixing bugs and enjoying Christmas

To consolidate my learning I have started making an application for myself. I try to be organised with my finances and keep a fairly keen eye on them but when budgeting I find it hard to put a figure on what I spend on food per month.

Keeping a pen and paper to write down every time I bought something from Tescos was a fruitless endeavour so why not make an iPhone application? I always thought it would be a nice simple project to start with, basic user interaction and some data persistence.

So using what I had learnt in the first few days I created a basic user interface and put in the logic to count and it worked nicely. Also what was quite pleasing was that it was pretty easy, I had a non persistent version working in a few hours.

First

Data persistence

For this first version I only need to save the amount spent, which you would normally just save in a text file and this is possible on the iPhone. However most of the applications I will be writing will require a structured data storage using SQLite so I decided to skip a few chapters in the book and learn how to do that.

I am aware that the approach I have taken is quite hacky, the database table is created in code (bleurgh!) and the querying code is in the controller, rather than abstracted away elsewhere. But in principle it works and gives a grounding on how to do it.

Adding references

First of all you need to create a reference to the SQLite3 library in the project. Right click on the frameworks folder and select Add to project from the Project menu. Navigate to Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/IphoneSimulatorX.Y.sdk/usr/lib and add the file libsqlite3.dylib.

In the header file that you want to work with SQLite add #import "/usr/include/sqlite3.h" and add a pointer to the SQLite database sqlite3 *database;.

You will also need to define some kind of filepath for the database file. Use the NSSearchPathForDirectoriesInDomains to search for the directories in the iPhone’s directory and get the documents directory by taking the first element in the array returned. Then define a file name for it i.e “mydb.sqlite3″.

Creating the database

The method sqlite3_open takes a filepath to a database and the reference to a database pointer. If the database hasn’t been created, it will create one in the filepath defined, otherwise it just opens.

In Countify I then wrote an SQL statement into an NSString which created the table I wanted IF NOT EXISTS. To execute the code you use the method sqlite3_exec. It is worth noting that SQLite works with C strings, so you have to do [mystring UTF8String] in order for your strings to work.

Running queries on the database

The pattern for querying the database is to prepare a statement, binding different variables as you see fit, performing a step to execute the statement and then using the sqlite methods to read the current data shown.

So first you create a sqlite3_stmt pointer along with an NSString of your query. On to your query you can add ? characters which you can bind different values to, for example:

SELECT TOTALSPENT FROM SPENDING WHERE ID = ?"

Then after you have used sqlite3_prepare_v2 to prepare your sqlite3_stmt you can use the statement multiple times and simply bind different values to them. So imagine were using the above SQL statement to read through multiple spending records:

for(i blah blah whatever i want to iterate through){
   if(sqlite3_prepare_v2(database, [nsstringofquery UTF8String], -1, &pointerto_stmt, nil)==SQLITE_OK){
      sqlite_3_bind_int(pointerto_stmt, 1, i); //1 refers to the ? index, not zero based!
      sqlite3_step(pointerto_stmt);
      totalSpent= totalSpent+sqlite3_column_double(pointerto_stmt, 0); //0 refers to the column index of the row in the database
   }
}

I followed a similar pattern with updating the database.

So this is certainly a lot more long winded than using LINQtoSQL to say the least. But at the same time it is fairly simple and I dont think there will be any need for an elaborate database schema with any projects I do, so the methods I have described should be fine.

Done! For now.

I am quite pleased at how simple (relatively) this was. Admittedly it is a very basic application but it does work well. I am not going to make any additions to it for now, I have to learn other things which are less applicable for what I want to do and more about what I need to write for work. I have to make a video quiz application in under 6 weeks so I really have to work hard to achieve that.

I feel like I’m still not entirely sure how about how an iPhone application should be written from an architectural standpoint. Hopefully as I get more experience I will understand how to structure them so they are a bit more maintainable.

Next blog post will be about writing multiple view applications.

12 Jan, 2010

Work in 2009

Posted by: Chris In: Programming

I have completed another year of working with .NET and other technologies and below I will summarise what I have achieved. It has been a good solid year, I have some more feathers in my cap and more meaningless acronyms to add to my CV.

AIDS Images

I talked about this in detail in a previous post. I would say this is the biggest programming project I have undertaken. I had to work with 2 other teams to get this to work and I was lucky enough to fly out to Geneva to meet the client.

The website is a database of AIDS images collected by the client. It has nice web 2.0-y features like drag and drop using javascript and other AJAX magic. The back end was build using ASP MVC 1, which was building on my knowledge of writing CoDriver (our in-house issue tracking system). This was the first project that I tried to really develop using test driven development

AIR Dictionary

In my previous year I had created some pure javascript dictionaries that can be plugged into a web page with one line of Javascript. It was decided that I could take this concept and create an Adobe AIR application for more dictionaries.

The main challenge of this project was ensuring that it would be simple to deploy either web based or air dictionaries, given an XML file of dictionary data. I wrote an importer in C# which converted the XML into compressed JSON. The dictionary Javascript then performed AJAX requests to get definitions from a web service that I also built.

There was a central Javascript library which did most of the business logic and to handle the differences between AIR and normal web pages there were Javascript files to call the library functions and render.

iPhone Development

Towards the end of the year we were told that sales would require us to be able to create iPhone applications. Specifically “Video Bytes” which will be medical quizzes and medical dictionary applications. When I have had time I have tried to learn Objective C with a view to creating these applications.

It’s a nice change of pace from .NET, going back to essentially C which is what I studied at university.

Summary of skills gained

Test driven development

I really like the idea of TDD and it does save a lot of time in retesting and I have witnessed how it does give you more confidence to refactor your code. So how come I always seem to end up lacking the discipline to actually do it all the time?!

As I mentioned the AIDS website was the first time I really tried this. Unfortunately due to pressure from the client our timescales got reduced quite dramatically. At this point I decided to go with what I was familiar with just to get the site done in time. This was frustrating and perhaps I should have been braver.

I have found that after doing TDD for a significant amount of time, that I am writing my methods expecting it to be tested, even if it isn’t. What I mean by this is that it has encouraged me to write methods which are short, self descriptive and self contained - even when I am not testing. I knew this to be a good habit before doing TDD but it seems to have been hammered into me more now.

ASP MVC

I built on my skills and experience gained in the previous year which has lead to the view and controller code being a lot leaner and easier to understand.

Dependency injection

I was reading a number of blogs on how to do TDD effectively and a recurring theme was to use dependency injection to make your business classes easier to test by separating the dependency on data access.

This worked out really well for me and I can definitely see the benefit in it.

Improved Javascript

The dark days of Javascript where there were no fancy libraries and no firebug mentally scarred me and made me frightened of using Javascript for years. I have been forced into embracing Javascript, mainly as any kind of web development without some kind of fancy AJAX effects is quite archaic.

Making AIR applications is more or less 99% Javascript too so I had to pull my finger out.

I still feel slightly frightened of Javascript, I think I am so used to strongly typed C# catching all my mistakes that I will always be a little wary of using them, although I appreciate the advantages that come from it.

Objective C

I am very much a fledgling objective C coder, but it pleases me that I get to learn it on the job. It’s refreshing to be using a completely different language and this will allow me to not only make iPhone apps, but possibly normal Mac OS X apps too.

Next year

Next year promises more .NET development as usual. I plan to learn ASP MVC2 and this will probably come by rewriting my blog in .NET.

Time permitting I will be writing iPhone applications, including dictionaries (again!) and a video quiz.

Our team is always talking about how we want to rewrite elements of our content management system, migrating away from WebForms, integrating new web 2.0 features using more modern javascript frameworks, rather than Microsoft’s one and using TDD. Whether we get the time and justification to do this is another matter but it would certainly be a very exciting project to do.

Tags:

04 Jan, 2010

New years resolutions 2010

Posted by: Chris In: Random thoughts

After looking at my new years resolutions of last year I achieved NONE OF THEM. So I forgive you if you dont read this as I am obviously full of shit.

However, I want this set in stone on the internets to remind myself of how I am a failure when I look back this time next year.

Learn more things to cook

In 2009 I found that I actually quite like cooking, despite being a bit cack-handed at it. For Christmas I was given two cooking books so this should help me out. I made this resolution last year but stupidly said that I should blog about them, which sort of added an extra burden to the resolution which meant I didnt do it.

I’m finding that by learning to cook more and more I am wasting less food as I get a little creative with what ingredients I have. Great for these tough economic times.

Make an iPhone app (of my own)

I have been lucky enough that I am allowed to sit down and get paid to learn Objective-C whilst working, when I dont have other projects to deal with. I have a few ideas for some iPhone applications, most of which are quite simple so I dont see why I wont be able to make them.

Maximise my profits!

I have to say, although the design of moneysavingexpert is awful, it is actually a pretty nifty site if you can bear to navigate through it. What it has made me aware of is how I can be more frugal with money and save some cash.

I have got a nice cashback credit card and I have finally bothered to get loyalty cards to places I frequently shop at. Along with checking for offers at the supermarket and learning how to make the most of the food I buy (rather than letting it go off) I can hopefully save myself a lot of money.

Lose a bit of weight

Ever since doing my knee in again after the Springer Cup, I have found it very hard to be motivated to eat sensibly and exercise. This has resulted in me putting on a bit of weight. I really aught to buck my ideas up and lose a bit of weight.

This shouldnt actually be too hard if I get back in the routine of going to the gym every week and trying to eat a little less cheese a week. It’s not like I am grossly overweight, I just need to eat a few less pies.

IMG_4292
Less cake for me, unfortunately

I have a feeling that after I have my operation this will happen sort of naturally as exercise is a big part of physiotherapy - which I have at least a year of.

Re-write my website in .NET

The last time I updated my WordPress install (the thing that powers this blog) I broke everything. I know how to write PHP but I have very little desire to touch it and I cannot be bothered to learn the WordPress architecture.

I know that if I write the site from the ground up I’ll be able to do a lot more experimentation and be able to add new features easier, freeing myself from the shackles of apathy.

It’ll also let me do some more frontend designy work which I miss somewhat.

I think of all the resolutions this is the least likely I will be doing, as I already have a website I am supposed to be making in my free time.

Flickr PhotoStream

  • yyeeeeeaaah
  • tigerlols
  • GENGAR LIKES LOLA
  • othercupcake

About

Hello, I am Chris James, I am a programmer based in London. I enjoy standards based web development, working in .NET with C#. This blog is mainly a diary for me but I occasionally will post about web development issues when the mood suits me.

My places on the internets