7. Before we start...
• Switch OFF phones
• Been here is funny
• ¡Live the moment!¡Carpe diem!
8. Before we start...
• Switch OFF phones
• Been here is funny
• ¡Live the moment!¡Carpe diem!
• Ask me a lot. Don’t yawn
9. What you need (checklist)
• a Mac with OS X capable of running Xcode 4.6.1
• last Xcode 4 installed 4.6.1
• You can also use prerelease software, if you are a registered Apple developer.
No support then, sorry :-D
• SimPholders installed: http://simpholders.com
• SQLLite database browser: http://sqlitebrowser.sourceforge.net
• (optional) set $HOME/Library folder visible, using (from a Terminal)
10. Diego Freniche / http://www.freniche.com
Idea: Creating the Core Data Stack
11. Diego Freniche / @dfreniche / http://www.freniche.com
The Core Data Stack
Managed Object Context
Persistent Store Coordinator
Persistent Object Store
Managed Object Model
13. Diego Freniche / @dfreniche / http://www.freniche.com
Doubts
• Use Apple’s code?
14. Diego Freniche / @dfreniche / http://www.freniche.com
Doubts
• Use Apple’s code?
• Really?
15. Diego Freniche / @dfreniche / http://www.freniche.com
Doubts
• Use Apple’s code?
• Really?
• Use a singleton?
16. Diego Freniche / @dfreniche / http://www.freniche.com
Doubts
• Use Apple’s code?
• Really?
• Use a singleton?
• Don’t use a singleton?
17. Diego Freniche / @dfreniche / http://www.freniche.com
Doubts
• Use Apple’s code?
• Really?
• Use a singleton?
• Don’t use a singleton?
• Use dependency injection?
18. Diego Freniche / @dfreniche / http://www.freniche.com
Apple’s code
19. Diego Freniche / @dfreniche / http://www.freniche.com
Apple’s code
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Test" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Test.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
20. Diego Freniche / @dfreniche / http://www.freniche.com
Apple’s code problems
• Core Data Stack inside AppDelegate?
• Really?
• Separation of concerns?
• Only one Managed Object Context
21. Diego Freniche / @dfreniche / http://www.freniche.com
Create our own Core Data Stack
22. Diego Freniche / @dfreniche / http://www.freniche.com
Create our own Core Data Stack
• In one “simple” method
23. Diego Freniche / @dfreniche / http://www.freniche.com
Create our own Core Data Stack
• In one “simple” method
• Singleton / not singleton?
24. Diego Freniche / @dfreniche / http://www.freniche.com
Create our own Core Data Stack
• In one “simple” method
• Singleton / not singleton?
• Use both!
25. Diego Freniche / @dfreniche / http://www.freniche.com
Dependency injection? Or singletons FTW?
• It depends :-)
26. Diego Freniche / http://www.freniche.com
Idea: using asserts to check threads
27. Diego Freniche / http://www.freniche.com
Asserts
• Check if we are running UI code in the UI Thread
• Check if we are NOT running Core Data code in the UI Thread
28. Diego Freniche / http://www.freniche.com
Asserts
• Check if we are running UI code in the UI Thread
• Check if we are NOT running Core Data code in the UI Thread
#define DF_ASSERT_MAIN_THREAD [NSThread isMainThread]?:(NSLog(@"NOT IN MAIN
THREAD"),abort())
29. Diego Freniche / http://www.freniche.com
Idea: create a common UITableView/Core data
class
30. Diego Freniche / http://www.freniche.com
Idea: use an NSManagedObject subclass
31. Diego Freniche / @dfreniche / http://www.freniche.com
Entities Design Tips
32. Diego Freniche / @dfreniche / http://www.freniche.com
Entities Design Tips
• Always add field order
33. Diego Freniche / @dfreniche / http://www.freniche.com
Entities Design Tips
• Always add field order
• Try to create a good UML diagram at first
34. Diego Freniche / @dfreniche / http://www.freniche.com
Entities Design Tips
• Always add field order
• Try to create a good UML diagram at first
• Have an NSString constant with every Entity’s name inside .h
35. Diego Freniche / @dfreniche / http://www.freniche.com
Extend NSManagedObject
• Editor > Create NSManagedObject subclass...
• creates @dynamic properties
• getter / setter generated in runtime (@property in compile time)
• Core Data doesn’t know at compile time if the persistent store is going to
be XML or a DB (or in-memory)
36. Diego Freniche / @dfreniche / http://www.freniche.com
Extend NSManagedObject
• overwrite init to call designated initializer
38. Diego Freniche / @dfreniche / http://www.freniche.com
Validate Properties
• One for every property, if we want it
• Passing parameter by reference
• It should return YES if validation is passed
39. Diego Freniche / @dfreniche / http://www.freniche.com
Validate Properties
• One for every property, if we want it
• Passing parameter by reference
• It should return YES if validation is passed
-(BOOL)validateName:(id *)ioValue error:(NSError * __autoreleasing *)outError;
40. Diego Freniche / @dfreniche / http://www.freniche.com
Validator for operations
• First thing: must call [super ...]
• Useful to check business rules (using several properties)
41. Diego Freniche / @dfreniche / http://www.freniche.com
Validator for operations
• First thing: must call [super ...]
• Useful to check business rules (using several properties)
- (BOOL)validateForDelete:(NSError **)error
- (BOOL)validateForInsert:(NSError **)error
- (BOOL)validateForUpdate:(NSError **)error
42. Diego Freniche / @dfreniche / http://www.freniche.com
Support for KVO
• Good for Faults
43. Diego Freniche / @dfreniche / http://www.freniche.com
Support for KVO
• Good for Faults
- (void)willAccessValueForKey:(NSString *)key
45. Diego Freniche / @dfreniche / http://www.freniche.com
Mogenerator
created by Jonathan 'Wolf' Rentzsch
46. Diego Freniche / @dfreniche / http://www.freniche.com
Mogenerator (quoting from the web page)
47. Diego Freniche / @dfreniche / http://www.freniche.com
Mogenerator (quoting from the web page)
• http://rentzsch.github.io/mogenerator/
48. Diego Freniche / @dfreniche / http://www.freniche.com
Mogenerator (quoting from the web page)
• http://rentzsch.github.io/mogenerator/
• generates Objective-C code for your Core Data custom classes
49. Diego Freniche / @dfreniche / http://www.freniche.com
Mogenerator (quoting from the web page)
• http://rentzsch.github.io/mogenerator/
• generates Objective-C code for your Core Data custom classes
• Unlike Xcode, mogenerator manages two classes per entity: one for
machines, one for humans
50. Diego Freniche / @dfreniche / http://www.freniche.com
Mogenerator (quoting from the web page)
• http://rentzsch.github.io/mogenerator/
• generates Objective-C code for your Core Data custom classes
• Unlike Xcode, mogenerator manages two classes per entity: one for
machines, one for humans
• The machine class can always be overwritten to match the data model,
with humans’ work effortlessly preserved
51. Diego Freniche / @dfreniche / http://www.freniche.com
Installing mogenerator
52. Diego Freniche / @dfreniche / http://www.freniche.com
Using it
• it’s a script, so we can launch it from command line
• using iTerm, DTerm, etc.
• Best way: to have it inside our project
• Create a new Aggregate Target (New Target > Other > Aggregate)
• Add Build Phase > Add Run Script
53. Diego Freniche / @dfreniche / http://www.freniche.com
Using it
• it’s a script, so we can launch it from command line
• using iTerm, DTerm, etc.
• Best way: to have it inside our project
• Create a new Aggregate Target (New Target > Other > Aggregate)
• Add Build Phase > Add Run Script
mogenerator --template-var arc=true -m RetroStuffTracker/
RetroStuffTracker.xcdatamodeld/RetroStuffTracker.xcdatamodel/
54. Diego Freniche / @dfreniche / http://www.freniche.com
Two classes
• _MyClass.*: machine generated
• *MyClass.*: human edited
• Never, ever recreate the classes
again from the Core Data Model
55. Diego Freniche / @dfreniche / http://www.freniche.com
Two classes
• _MyClass.*: machine generated
• *MyClass.*: human edited
• Never, ever recreate the classes
again from the Core Data Model
59. Diego Freniche / http://www.freniche.com
Idea: use Magical Record
60. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
61. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
• Just a bunch of categories to help you write less code
62. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
• Just a bunch of categories to help you write less code
• You have to know your sh*t
63. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
• Just a bunch of categories to help you write less code
• You have to know your sh*t
• CocoaPods friendly
64. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
• Just a bunch of categories to help you write less code
• You have to know your sh*t
• CocoaPods friendly
• Ideal: use Unit testing + Mogenerator + CocoaPods + Magical Record
65. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
• Just a bunch of categories to help you write less code
• You have to know your sh*t
• CocoaPods friendly
• Ideal: use Unit testing + Mogenerator + CocoaPods + Magical Record
• My point: 7 people, 7 ideas, all great
66. Diego Freniche / http://www.freniche.com
Magical record != avoid Core Data at all costs
• Just a bunch of categories to help you write less code
• You have to know your sh*t
• CocoaPods friendly
• Ideal: use Unit testing + Mogenerator + CocoaPods + Magical Record
• My point: 7 people, 7 ideas, all great
• all different