Why MonoTouch
I’m working on bringing some of my works over to the iPhone and iPad and in doing so I decided to use MonoTouch (since I have a strong .NET background and absolutely no Objective-C experience other than some evaluation work). The long term plan is to master MonoTouch, which will familiarize me with Cocoa, iPhone/iPad idiocies, and Interface Builder. After that, I’ll hop over and learn Objective-C and move all dev over there. Right now though, its all about getting an app done.
My Problem
I have a SQLite DB that contains a few thousand records that need to be seeded before users can use my app. I have two options at this point:
- Create the SQLite DB and then run some SQL commands against it to seed the DB.
- Create a SQLite DB and put it into the app and then copy it to the destination at runtime.
Option 1 is probably what most people will do.
- Pros: Lots of common documentation on how to do this.
- Cons: This can take some time to load and can be problematic as iOS can kill your process, users don’t want to wait, etc.
Option 2 is what not many people do in MonoTouch (I’m assuming), but would like to.
- Pros: IT’S FAST. Its a simply copy file procedure and then I can immediately run my app.
- Cons: Not much documentation on HOW to do this. I hope this post will fix that.
I have opted for option #2. Unfortunately there is no documentation saying HOW to do this in MonoTouch (that I can find). In the end, it’s a fairly simple process. Here’s how I went about it.
How To Copy a Pre-loaded (seeded) DB from the App content to iOS at RunTime
- Create your SQLite db in your favorite SQLite editor. There’s a lot of them out there (Google is your friend).
- Place your DB into your MonoTouch project as shown in Figure 1.
- Mark the DB as “Content” as shown in Figure 2.
- On the FinishedLaunching event in the Main.cs file, enter the code shown in Listing 1 (or better yet, create a class for it).
Figures
Figure 1: Add the db file to the project. The “workouts.db3” is my DB file.
Figure 2: Set the Build action to “Content”
Listing 1: Code that will copy the “workouts.db3” from the application package into the iOS folder so you can use it.
public void CopyDb () { string dbname = "workouts.db3"; string documents = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // This goes to the documents directory for your app string db = Path.Combine(documents, dbname); string rootPath = Environment.CurrentDirectory; // This is the package such as MyApp.app/ string rootDbPath = Path.Combine(rootPath, dbname); Console.WriteLine("Root Db Path: " + rootDbPath); Console.WriteLine("Final Db Path: " +db); if(File.Exists(db) == false) { Console.WriteLine("Copying DB!"); File.Copy(rootDbPath, db); } else { Console.WriteLine("DB Exists, not copying."); } }
When FinishedLaunching is called, I call “CopyDb” and this method takes care of the rest. What it does is check for existence of the db and if it exists it will NOT copy it. If it does not, it will copy it. The only time this should be run is the first time the user installs the app.
If you’re having a hard time understanding the iOS file structure, I highly recommend reading Rory Blyths explanation on this SO post. He covers the basics of the iOS sandbox very well. Thanks for the post Rory.
All Done
You should now be set to connect to the db using the SQLite code you’ve see in other MonoTouch examples.
Anonymous says
step to step methods that are told very efficiently bu6 at some steps i am not gonna be agreed to to admins may be they will be right in their option and i am in mine!
regards@work experience insurance cover
Coach Factory Outlet says
http://buycoachfactoryoutletsz.com
Nicolaj says
very cool approach!!
Daniel Sovino says
i´ve should have seen this 6 hours ago….
Topota says
really helpful! Thanks..