Archive for tag: MonoTouch

MonoTouch SQLite Performance Tip

DataReaders are the fastest ways to access data in .Net, and most MonoTouch SQLite tutorials use them to access SQLite data using the following  syntax:

string variable = reader["MyDBField"];

This syntax is very easy to read however it doesn't perform very well.  The data reader has several inbuilt methods for reading values including:

  • GetInt32()
  • GetDouble()
  • GetString()

Using these methods improved the performance of my MonoTouch - SQLite application by a little over 60%.  The only downside is that you need to access the data colums using their ordinal values. i.e.

string variable = reader.GetString(0);

This can be a little harder to maintain, however provided you aren't using "select *" syntax, the extra effort will result in a nice performance increase.

 

mtouch failed with no output solved

Whilst trying to build my latest application I came across a strange "mtouch failed with no output" error.  The error would only occur when building for the iPhone (not the simulator) and despite checking all the forums I was unable to find a solution that would work.

There are a number of forum posts highlighting that spaces in folder paths will cause MonoTouch builds to fail, however it also appears that having a space in the Project name will also cause this to occur.

Screen shot 2010-04-06 at 20.39.23

When I tried to build my project with a space in the Name (as shown in the screenshot above) the build would fail.  Removing the space solved the problem.

Finding nearby locations with iPhone GPS and MonoTouch

Whilst developing my latest application I was faced with the issue of finding nearby locations from a SQLite database containing over 60,000 records.  Each record contained a latitude, longitude and location name.

The problem was how to quickly find the closest locations without having to calculate the distance between the current location and target location for every record.

The solution I came up with has two parts.  First, I applied a limit clause to my select statement to only return the top 100 rows within +/- 1 degree of my current latitude and longitude.

One degree equals approximately 111km (61 miles) so by filtering for +/- 1 degree we are effectively selecting the top 100 records within a 222km radius of the current location.

It is then a relatively simple task to calculate the distance between the current location and selected locations and order by distance.

Monotouch SQLite Example

Using SQLite databases with MonoTouch iPhone applications is surprisingly easy to do.  For this example I have created a small application which queries a SQLite database and displays a list of country names in a UITableView.  The sample project can be downloaded here.

Step 1 is to create an SQLite database.  To do this I use the free "SQLite database browser" tool.  As you can see I have created a single table with two fields and inserted a number of rows (all the countries I have worked in).

SQLite Database Browser

Once you have created the database, add it to your project and then set the properties for the database file as follows:

  • Build Action = Content
  • Copy to output directory = Copy Always

Next you need to add references for:

  • System.Data.dll
  • Mono.Data.SQLite.dll

After that the data access code is pretty straight forward as can be seen below:

var conn = new SqliteConnection("Data Source=ExampleDB");

using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText ="select CountryID, CountryName from Countries";
using(var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Country c = new Country(int.Parse(reader["CountryID"].ToString()),
(string)reader["CountryName"]);
_Countries.Add(c);
}

}
}

As you can see, the database access code is fairly standard! Run the sample project in the simulator and you should see the database contents displayed.

Simulator

IPhone Programming Books

There have been a number of tweets and forum posts recently about the best IPhone development books for people starting out with MonoTouch.  I have recently purchased three APress IPhone dev books from Amazon all of which have great material for someone starting out with IPhone development.

I would highly recommend these three books and you can see from the other customer reviews on Amazon that I am not alone.  Click on the books below to go to Amazon and read the other customer reviews.

Accessing GPS data on the IPhone with MonoTouch

A lot of .Net developers have probably heard about MonoTouch but are still concerned about how 'hard' it might be to create a useful application.  One of the applications I am currently working on uses the IPhone's inbuilt GPS and I wanted to show not only how easy it is to get this data, but how understandable the code will be for anyone with a .Net background.

First we add a reference to MonoTouch.CoreLocation with the standard 'using' syntax;

using MonoTouch.CoreLocation;

We then create a module level instance of the location manager:

private CLLocationManager locManager = new CLLocationManager();

We can start the GPS location tracking:

locManager.StartUpdatingLocation();

After which we can access our latitude and longitude:

if (locManager != null)
{
  string lat = locManager.Location.Coordinate.Latitude;
  string lng =locManager.Location.Coordinate.Longitude;
}

This article is based on information I initially read on the following University of Texas at San Antonio blog.

IPhone Development with MonoTouch

If you are a .Net developer and would love to be able to develop apps for the iPhone without learning Objective-C then I may have your solution.

iphonesmallSome  of you may be familiar with an open source project called Mono which was aimed at making .Net applications run on the linux platform.  A similar project has now been built by Novell called MonoTouch, which allows iPhone applications to be written in .Net.

Having used the project for the past three months, I have to say that I am very impressed.  It is still a bit of a learning curve at the start and the current online help is very limited, however if you can stick with it the results are amazing.

The biggest issue for most .Net developers is going to be the fact that you still need an Mac to run MonoTouch.  Also, although MonoTouch is free to download and run on a simulator, you will need to pay 399 euro to register it before you can download apps to your phone.