Wednesday, July 30, 2008

Core Data


I've been exploring Core Data this morning. There's a great set of videos that make up a tutorial to build the project shown above. The only code in the whole thing is down at #13. Here is my cheat sheet (it's handy for debugging the result).

1.  Create New Project => Core Data Doc App and call it CheckIn.
2. Model data.
Open MyDocument.xcdatamodel
Two entities:
VersionRelease, CommitMessage
3. VersionRelease has 1 attribute:
name: string default = untitled
and 1 relationship
commitMessages: destination CommitMessage
To-Many Delete Rule = Cascade
4. CommitMessage has 3 attributes:
message: string default = new commit message
creationDate: Date
isCommitted: boolean default = NO
and 1 relationship
versionRelease: destination VersionRelease
Inverse = commitMessages
5. User Interface.
Open MyDocument.nib
NSTableView with 1 column titled Releases
Two Buttons 25 x 23
6. NSArrayController called ReleasesController
manages entity VersionRelease
Bind managedObjectContext to File's Owner
managedObjectContext
Bind Table Column value to ReleasesController
arrangedObjects w/path = name
Set Buttons target-action to ReleasesController
add and remove
7. Test
8. Another TV with 1 column titled Commit Messages
Below that a text field, a date picker
and a checkbox called is Committed
Two Buttons
9. NSArrayController called MessagesController
manages entity CommitMessage
Bind managedObjectContext to File's Owner
managedObjectContext
Bind contentSet to ReleasesController
selection w/ path = commitMessages

Bind Table Column value to MessagesController
arrangedObjects w/path = message

Bind TextField value to MessagesController
selection w/path = message
(contrary to audio, not the "same thing")

Bind DatePicker to MessagesController
selection w/path = creationDate
Bind CheckBox to MessagesController
selection w/path = isCommitted
Set Buttons target-action to MessagesController
add and remove
10. Test
11. Enhancements
TimeStamp
12. Open MyDocument.xcdatamodel
Change CommitMessage Class (top left) to CommitMessageMO
13. new File Objective-C class CommitMessageMO.m
In .h change NSObject to NSManagedObject
In .c add method:
- (void)awakeFromInsert
{
[self setValue:[NSDate date] forKey:@"creationDate"];
}
14. SearchBox
in MyDocument.nib put in a searchField
Bind Predicate to MessagesController
Customize predicate format (case-insensitive):
message contains[c] $value