I have recently been learning Objective-C and iOS and have had some success. One of the things I wanted to try when starting off with iOS was to see how well MonoTouch would work for me, and I have had some success with that as well. Where I haven’t had much luck is with the CoreData bindings that were introduced in version 3.0.12.
The biggest issue for me was the complete lack of any samples, if you do know of any please let me know.
This post will be about my initial discovery of how you can use CoreData with MonoTouch.
Create the Model
The first thing I determined was that it appears that you will need to create your object model in code and cannot rely on the model editor in Xcode to do any of the boiler plate code for you. In order to use the .xcdatamodel that you might have in an Xcode project you need to use the compiled output, a .momd file that is built as part of the Xcode build process. Without knowing how to do this independently all you can do is build up the model in code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Two things to note here, the property EntitiesByName on the model object doesn’t appear to have been updated or isn’t bound correctly because it always returns an empty NSDictionary. Debugging similar code in Xcode reveals that this dictionary is updated, but we don’t appear to have access to it from MonoTouch. The other thing to note is that I didn’t have to set entity.ManagedObjectClassName to anything like I would have in Objective-C.
Create the Persistent Store Coordinator
The next bit of code required is a persistent store coordinator, we can do that quite easily.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
In Objective-C examples, calls to AddPersistentStoreWithType often have nil as the configuration parameter, here we use String.Empty because MonoTouch will throw a null reference exception if we don’t. The configuration name that we use here has to match the configuration name that we gave in SetEntities when we created the model.
Now all we need is a context manager..
1 2 3 4 5 6 7
This is the basic setup we need to be able to use CoreData, the next steps we need are a way to create objects and get them out again.
Inserting Objects into the Context
At first I tried to copy the methods in Objective-C examples by using
but there appears to be issues with the bindings for that method. In MonoTouch, the return type for InsertNewObjectForEntityForName returns an NSEntityDescription but this method creates an NSManagedObject which results in invalid cast exceptions. I had to create instances of NSManagedObject directly. Creating an NSManagedObject requires the NSEntityDescription for the entity you want created and the NSManagedContext into which the object will be placed. I discovered that the model didn’t appear to have anything in the EntitiesByName dictionary, so we can’t use that. So I use something like
1 2 3
Setting attribute values the object is done by using the SetValue method
Getting Objects Out
To retrieve our objects from the SQLite database we use an NSFetchRequest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Now, there are some plenty of other things to learn before we can really use this, NSPredicate for filtering the data; retrieving the second batch of records, versioning and migration just to name a few that spring to mind, but I hope that this might serve as a starting point for anyone else who is new to iOS and MonoTouch like me.