Skip to main content

iOS Persisting Data Part 2

So I started trying to find a way to save data in my app. I have been watching this PluralSight course to help me along, Core Data Fundamentals, by Brice Wilson. Having thoughts of EF Code first in my head I thought things were going to be fairly straight forward after setting up my Model objects and then a connection to the data store. However, after getting those pieces setup the course shows some simple object creation and data retrieval. This ended up being nothing like I expected. Every call to and from the data store used strings to find objects and set or get properties. Yikes! I envisioned defining an object and then being able to use intellisense for that object and all of its properties. There is more to the course so hopefully we will get to that point, but if that is the case, this seems like an awful way to introduce using Core Data.

Continuing on with the course I find that the reason my original objects do not provide any intellisense is that they are NSManagedObject objects. This would be similar to casting all of the EF objects to IEntity, the IEntity interface wouldn't have knowledge of the properties of a concrete implementation. So it appears that I will need to make a subclass of NSManagedObject for each object that I want to store and set my model's mapping to that class instead of leaving it as the default.

Creating these subclasses ends up being super easy. Because I already have my Model objects defined, all I need to do is add new classes of type NSManagedObject subclasses. I am wizarded through choosing my Model and the objects that I want to subclass. This creates a class for each of my objects with property definitions for all of their properties. Then using the new classes I can set and get these objects and properties through intellisense just as a would any other defined class. I really do not see at this point why he showed off the other method first as this is a much more simple way to interact with objects, you don't need to remember what the property name was or worry about misspellings when the IDE can autocomplete them for you.

Coming back to my thoughts of this being like EF Code-First, it turns out to be more like EF DB-First. You need to have your Model defined and have its properties defined, then Core Data can generate a file for that class with its properties. If you want to extend that class then you need to use a Category, which appears to be sort of like a Partial class. You are able to create a new Interface that adds to an existing Interface by using the + symbol. Thats a little weird, and probably going to take some getting used to.

So using CoreData isn't exactly like using an ORM, although there are some similarities. There are some oddities that I may just not know the best approach for yet, like fetching an object via its string name instead of asking for the object that maps to that store. (Update, thanks to David, I was able to use: NSStringFromClass([ClassType class]), as a way to replace the "Magic Strings" I had littered throughout my project calling my objects.  This provides me with a much safer way of dealing with objects from the data store.

Now I have data being persisted into a SQLite DB. I am able to retrieve that data and iterate over it. Next up will be doing some logic with these objects, which means back to Testing and TDD, Hooray!


Popular posts from this blog

Converting a Large AngularJS Application to TypeScript Part 1

I work on a project that uses AngularJS heavily. Recently we wondered if using a preprocesser like CoffeeScript or TypeScript for our JavaScript would be beneficial. If our team is going to switch languages, we would need to be able to convert existing code over without much pain and we would have to find enough value in switching that it would be worth the conversion. I had read an article that stated that because TypeScript is a SuperSet of JavaScript, you could convert a plain JavaScript file to TypeScript by changing the extension to .ts and not much else would need to change. I wanted to test out this claim, so I took a file that I was familiar with, an Angular Controller, and tried to convert it to TypeScript to see how much effort it would take and then try to figure out where we would benefit from using TypeScript. This is what the controller JavaScript file looked like to start out with: ( function () { 'use strict' ; angular .module( 'app'

Gamify TDD

I like it when things that would not normally be associated with games add concepts from games as a way to incentives you to accomplish things. Why simply go for a run if you can have an app that will track you and give you a gold star if you do better than you did the last time? Why go to the coffee shop that only gives you coffee if the other one will give you points that you can redeem for free drinks eventually? I was recently introduced to C odeSchool , an online training system similar to PluralSight, it has video courses and challenges you can take to prove that you retained what the video taught. CodeSchool also adds badges and tracks to your learning, so as you complete a video and its challenges you get a badge. Complete a collection of courses within a specific discipline and you become a master of that discipline. Some of these incentives are not tangible and really don't mean much in the real world, but they tend to work for me. If I start working towards a large g

Converting a Large AngularJS Application to TypeScript Part 2

In part 1 I was able to take an Angular controller written in JavaScript and convert it to a TypeScript file while doing very little to change the code. In this post I am going to explore transitioning that same controller to actually use the features provided in TypeScript. This is how I left off my controller: declare var angular: any; (function () { 'use strict'; var controller: any = function($scope){ ... } angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; })(); While performing the translation from JavaScript to TypeScript, I would make sure at every step that the functionality I expected still worked, so if anything I did broke the system I would change it back and try again with another approach. Also if something seemed like it worked too easily, I would break it on purpose to make sure I wasn't getting a false result through browser caching a previously working