01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Beyond the basics<br />

251<br />

How do you use your new model classes now? We look at the addTask method in<br />

TasksViewController.m together, and you’ll be able to convert the rest of the application<br />

yourself. If you get stuck, check out the code for the completed project online.<br />

First, make sure you import Person.h and Task.h and that you change the person<br />

instance variable type from NSManagedObject to Person. The new addTask method<br />

will look like the following listing.<br />

Listing 12.16<br />

Modifying addTask in TasksViewController.m to use Person class<br />

- (void)addTask {<br />

Task *task = [NSEntityDescription<br />

insertNewObjectForEntityForName:@"Task"<br />

inManagedObjectContext:[person managedObjectContext]];<br />

task.name =<br />

[NSString stringWithFormat:@"Task %i", [person.tasks count] + 1];<br />

task.isDone = [NSNumber numberWithBool:NO];<br />

[person addTasksObject:task];<br />

}<br />

NSError *error = nil;<br />

if (![[person managedObjectContext] save:&error]) {<br />

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);<br />

} else {<br />

}<br />

That looks much nicer, doesn’t it? And notice that the addTasksObject: method of<br />

the Person class is used to add new tasks to the current person. That makes your code<br />

a lot more readable, and it’s much clearer to see what’s going on. It won’t be a problem<br />

for you to change the rest of the code to use the new model classes.<br />

12.5 Beyond the basics<br />

You’ve built your first Core Data application. You know how to create a data model<br />

and how to fetch, save, and even filter and sort data. That’s really all you need to know<br />

to use Core Data in your own applications. As mentioned earlier, however, Core Data<br />

is a complex framework, and there’s still more to learn. It’s beyond the scope of this<br />

chapter to cover Core Data in depth, but in the next three sections, we touch on a few<br />

important topics and provide further resources to point you in the right direction.<br />

12.5.1 Changing the data model<br />

Change is the one thing that’s constant in every software project. The day will likely<br />

come when you’ll have to add a property to an entity, add a new entity, or add a new<br />

relationship to your data model. When you do, you’ll have to take some extra steps to<br />

ensure that your user’s data will still work with the new version of your application.<br />

You won’t change your data model—instead, you’ll add a new one but keep the old<br />

version so it can read data that was saved with that version of your data model.<br />

Data migration can be a complex task, but when the change to the data model<br />

is small (for example, adding a new property or making a non-optional property

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!