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 />

253<br />

All you do is pass in two options that tell Core Data to try to automatically determine<br />

what has changed between the data models and to try to automatically migrate the data.<br />

Build and run it again—everything should work.<br />

If you can, you should try to avoid substantial changes to your data model, especially<br />

after you’ve shipped your application and people are already using it. Put some<br />

time and thought into your data model before you start coding.<br />

If you need to perform a more complex migration, check out Apple’s Core Data<br />

Model Versioning and Data Migration Programming Guide (http://developer.apple<br />

.com/iphone/library/documentation/Cocoa/Conceptual/CoreDataVersioning/<br />

Introduction/Introduction.html).<br />

12.5.2 Performance<br />

Using NSFetchedResultsController is already one of the best things you can do<br />

regarding performance of your iOS Core Data application. There are a few more<br />

things to keep in mind, though. If you know that you’ll always need access to a certain<br />

relationship of your entity, you should tell Core Data to fetch those child entities with<br />

the initial fetch request. Otherwise, Core Data would have to execute another fetch<br />

request for every entity. With 100 people, that would make 101 fetch requests (the initial<br />

one to fetch the people and one for each person to get the tasks). Using<br />

NSFetchResult’s setRelationshipKeyPathsForPrefetching: method avoids this overhead.<br />

In your example, to improve the performance when you fetch people, it would<br />

look like this:<br />

[request setRelationshipKeyPathsForPrefetching:[NSArray<br />

arrayWithObject:@"tasks"]];<br />

This code loads the tasks right away along with the people.<br />

Another performance concern is binary data: unless the data is very small (smaller<br />

than 100 kb), you shouldn’t store it directly in the entity it belongs to. If the data is<br />

bigger than 100 kb, consider storing it in a separate entity and setting up a relationship<br />

between the data entity and the entity it belongs to (maybe a profile picture that<br />

belongs to a person). Anything bigger than 1 MB should not be stored in a Core Data<br />

entity but on disk as a file. Only the path to the file should be stored using Core Data.<br />

More information about performance can be found in Apple’s Core Data Programming<br />

Guide: http://developer.apple.com/mac/library/documentation/Cocoa/<br />

Conceptual/CoreData/Articles/cdPerformance.html.<br />

12.5.3 Error handling and validation<br />

You undoubtedly noticed that every time you save the managed object context, you<br />

pass in a pointer to an NSError object. So far, you haven’t really handled those errors.<br />

In a real application users have a right to be informed about anything that goes wrong<br />

with their data. So how do you handle errors in Core Data?<br />

If you encounter an error from which you can’t recover (can’t write data to disk or<br />

something catastrophic like that), you should inform the user about it with an

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

Saved successfully!

Ooh no, something went wrong!