Episode 13 - Archiving

Episode 13 - Archiving

Posted 02/06/2007 - 00:35 by cocoacast

undoIn previous episodes we pointed out that even though the document based applications know when changes are made if undo is implemented, we could not save the changes. In this Episode we will talk about how Archiving is used for saving files (Chapter 8 of the test book). In addition, we will discuss the Value Transformers and how they can be used with Cocoa Binding.



Here are the show notes for this Episode: Show Notes
View the Podcast

Show Notes:

Archiving your Application Data
Episode 13
Archiving -- a ways to save your models in files
Chapter 8
Archiving your Application Data
Episode 13
Archiving -- a ways to save your models in files
Chapter 8

Podcast Announcements
• New Website Design
• Challenges with Challenges from last episode
Podcast Announcements
• New Website Design
• Challenges with Challenges from last episode

Recap of the Last Episode
Summary of the Episode 12
Recap of the Last Episode
Summary of the Episode 12

Summary of Episode 12
• Adding Undo to Your Applications
• NSInvocation
• NSUndoManager
• RaiseMan with the Undo
• Key-Value Observing (KVO)
• Undo for Edits
• Windows and the UndoManager
Summary of Episode 12
• Adding Undo to Your Applications
• NSInvocation
• NSUndoManager
• RaiseMan with the Undo
• Key-Value Observing (KVO)
• Undo for Edits
• Windows and the UndoManager

In this Episode
• Challenges from last Episode
• Archiving
• NSCoder and NSCoding
• Encoding
• Decoding
• Document Architecture
• Info.plist and NSDocumentController
• NSDocument
• Saving and Loading
• More...
In this Episode
• Challenges from last Episode
• Archiving
• NSCoder and NSCoding
• Encoding
• Decoding
• Document Architecture
• Info.plist and NSDocumentController
• NSDocument
• Saving and Loading
• More...

Demo
Solution to Challenge -- Undo in Bindings version of Advanced RaiseMan
(as a bonus, we will discuss transformers and bindings)
Demo
Solution to Challenge -- Undo in Bindings version of Advanced RaiseMan
(as a bonus, we will discuss transformers and bindings)

Demo
Partial Solution to Challenge -- Undo in CoreData version of Advanced RaiseMan
Demo
Partial Solution to Challenge -- Undo in CoreData version of Advanced RaiseMan

Archiving
What is Archiving
Archiving
What is Archiving

Archiving
• Process of converting graphs of objects into stream of bytes and back
@"Boris"
NSStringexpectedRaise = 10Person@"Russ"
NSStringexpectedRaise = 20Person@"You"
NSStringexpectedRaise = 100PersonNSMutableArray012ArchivingUnarchiving01101010110Archiving
• Process of converting graphs of objects into stream of bytes and back
@"Boris"
NSStringexpectedRaise = 10Person@"Russ"
NSStringexpectedRaise = 20Person@"You"
NSStringexpectedRaise = 100PersonNSMutableArray012ArchivingUnarchiving01101010110

NSCoder and NSCoding
• Both are protocols (not concrete classes)
• NSCoding is uses as a promise to implement the following methods
- (id)initWithCoder:(Coder *)coder;
- (void)encodeWithCoder(NSCoder *)coder;
• NSCoder is abstraction of a byte stream
• Some of the concrete classes that confirm to NSCoder
NSKeyedUnarchiver -- reads objects from data stream
NSKeyedArchiver -- writes objects to data stream
NSCoder and NSCoding
• Both are protocols (not concrete classes)
• NSCoding is uses as a promise to implement the following methods
- (id)initWithCoder:(Coder *)coder;
- (void)encodeWithCoder(NSCoder *)coder;
• NSCoder is abstraction of a byte stream
• Some of the concrete classes that confirm to NSCoder
NSKeyedUnarchiver -- reads objects from data stream
NSKeyedArchiver -- writes objects to data stream

Encoding and Decoding
Using NSCoder to package and extract the data
Encoding and Decoding
Using NSCoder to package and extract the data

Encoding
• There are two types of encoding -- one for objects and one for primitives
• You may need to call [super encodeWithCoder:coder];
- (void)encodeObject:(id) forKey:(NSString *)aKey
• For primitives
- (void)encodeBool:(BOOL)boolv forKey:(NSString *)aKey
- (void)encodeDouble:(double)realv forKey:(NSString *)aKey
- (void)encodeFloat:(float)realv forKey:(NSString *)aKey
- (void)encodeInt:(int)intv forKey:(NSString *)aKey
Encoding
• There are two types of encoding -- one for objects and one for primitives
• You may need to call [super encodeWithCoder:coder];
- (void)encodeObject:(id) forKey:(NSString *)aKey
• For primitives
- (void)encodeBool:(BOOL)boolv forKey:(NSString *)aKey
- (void)encodeDouble:(double)realv forKey:(NSString *)aKey
- (void)encodeFloat:(float)realv forKey:(NSString *)aKey
- (void)encodeInt:(int)intv forKey:(NSString *)aKey

Decoding
• There are two types of decoding -- one for objects and one for primitives
• You may need to call [super initWithCoder:coder];
- (id)decodeObjectForKey:(NSString *)aKey
• For primitives
- (BOOL)decodeBoolForKey:(NSString *)aKey
- (double)decodeDoubleForKey:(NSString *)aKey
- (float)decodeFloatForKey:(NSString *)aKey
- (int)encodeIntForKey:(NSString *)aKey
Decoding
• There are two types of decoding -- one for objects and one for primitives
• You may need to call [super initWithCoder:coder];
- (id)decodeObjectForKey:(NSString *)aKey
• For primitives
- (BOOL)decodeBoolForKey:(NSString *)aKey
- (double)decodeDoubleForKey:(NSString *)aKey
- (float)decodeFloatForKey:(NSString *)aKey
- (int)encodeIntForKey:(NSString *)aKey

The Document Architecture
NSDocumentController, NSDocument, and NSWindowController
The Document Architecture
NSDocumentController, NSDocument, and NSWindowController

The Document Architecture
• NSDocument will act as a controller (MVC) and will be responsible for:
• Saving the model data to a file
• Loading the model from a file
• Displaying the model data in the views
• Taking user input from the views and updating the model
The Document Architecture
• NSDocument will act as a controller (MVC) and will be responsible for:
• Saving the model data to a file
• Loading the model from a file
• Displaying the model data in the views
• Taking user input from the views and updating the model

Info.plist and NSDocumentController
• Info.plist tells what types of files the application works with
• If the application is document-based, it creates instances of
NSDocumentController
• NSDocumentController handles New or Save All requests
Info.plist and NSDocumentController
• Info.plist tells what types of files the application works with
• If the application is document-based, it creates instances of
NSDocumentController
• NSDocumentController handles New or Save All requests

NSDocument
• Handling Save, Save As..., Save All, and Close requests
- (NSData *)dataRepresentationOfType:(NSString *)aType;
- (NSFileWrapper *)fileWrapperReperesntationOfType:(NSString *)aType;
- (BOOL)writeToFile:(NSString *)filename ofType:(NSString *)aType;
• Handling Open, Open Recent, and Revert To Saved requests
- (BOOL)loadDataRepresentation:(NSDate *)docDateofType:(NSString *)aType;
- (BOOL)loadFileWrapperReperesntation:(NSFileRepresentation *)wrapperOfType:(NSString *)aType;
- (BOOL)readFromFile:(NSString *)filename ofType:(NSString *)aType;
NSDocument
• Handling Save, Save As..., Save All, and Close requests
- (NSData *)dataRepresentationOfType:(NSString *)aType;
- (NSFileWrapper *)fileWrapperReperesntationOfType:(NSString *)aType;
- (BOOL)writeToFile:(NSString *)filename ofType:(NSString *)aType;
• Handling Open, Open Recent, and Revert To Saved requests
- (BOOL)loadDataRepresentation:(NSDate *)docDateofType:(NSString *)aType;
- (BOOL)loadFileWrapperReperesntation:(NSFileRepresentation *)wrapperOfType:(NSString *)aType;
- (BOOL)readFromFile:(NSString *)filename ofType:(NSString *)aType;

NSDocument
• When file opens, the data is read before nib file loads
• After file is read, document object is sent the following method
• Because of Revert to Saved command, need to update the UI
- (void)windowControllerDidLoadNib:(NSWindowController *)x;
NSDocument
• When file opens, the data is read before nib file loads
• After file is read, document object is sent the following method
• Because of Revert to Saved command, need to update the UI
- (void)windowControllerDidLoadNib:(NSWindowController *)x;

NSWindowController
• For each window opened, NSWindowController is created
• Usually not subclasses unless:
• You need more than one window for the same document (CAD app)
• You want the UI controller logic and model controller in separate classes
• You want to create a window without a corresponding NSDocument
NSWindowController
• For each window opened, NSWindowController is created
• Usually not subclasses unless:
• You need more than one window for the same document (CAD app)
• You want the UI controller logic and model controller in separate classes
• You want to create a window without a corresponding NSDocument

Demo
Saving and Loading with NSKeyArchiver and NSKeyUnarchiver
Demo
Saving and Loading with NSKeyArchiver and NSKeyUnarchiver

Demo
Setting Extension and Icon for File Type
Demo
Setting Extension and Icon for File Type

Preventing Infinite Loops
• You can have situations where object A points to object B,
while object B points back to object A
• In order to prevent loops objects encoded conditionally
- (void)encodeConditionalObject:(id)anObject forKey:(NSString *)aKey;
Preventing Infinite Loops
• You can have situations where object A points to object B,
while object B points back to object A
• In order to prevent loops objects encoded conditionally
- (void)encodeConditionalObject:(id)anObject forKey:(NSString *)aKey;

Versioning
• Versions are added to encoded stream so you can handle different
version of your app’s data
+ (void)setVersion:(int)theVersion;
unsigned version;
version = [coder versionForClassName:@”Person”];
Versioning
• Versions are added to encoded stream so you can handle different
version of your app’s data
+ (void)setVersion:(int)theVersion;
unsigned version;
version = [coder versionForClassName:@”Person”];

Create a Protocol
• Protocols are usually defined in .h files
@protocol Foo- (void)bar:(int)x;
- (float)baz;
@end
• Implementation of a protocol looks like this
#import “Rex.h”
#import “Foo.h”
@interface Fido:Rex
...etc...
@end
Create a Protocol
• Protocols are usually defined in .h files
@protocol Foo- (void)bar:(int)x;
- (float)baz;
@end
• Implementation of a protocol looks like this
#import “Rex.h”
#import “Foo.h”
@interface Fido:Rex
...etc...
@end

Document-Based Apps without Undo
• In order to turn the changes on call the method below
• NSDocumentChangeType could be one of the following:
• NSChangeDone -- increments change counter
• NSChangeUndone -- decrements change counter
• NSChangeCleared -- resets change counter to 0
• The window is dirty unless the change counter is 0
- (void)updateChangeCount:(NSDocumentChangeType)change;
Document-Based Apps without Undo
• In order to turn the changes on call the method below
• NSDocumentChangeType could be one of the following:
• NSChangeDone -- increments change counter
• NSChangeUndone -- decrements change counter
• NSChangeCleared -- resets change counter to 0
• The window is dirty unless the change counter is 0
- (void)updateChangeCount:(NSDocumentChangeType)change;

Challenges
• Fix/solve CoreData challenge from previous Episode
• Add saving to Advanced RaiseMan application
Challenges
• Fix/solve CoreData challenge from previous Episode
• Add saving to Advanced RaiseMan application

Summary
• Challenges from last Episode
• Archiving
• NSCoder and NSCoding
• Encoding
• Decoding
• Document Architecture
• Info.plist and NSDocumentController
• NSDocument
• Saving and Loading
• More...
Summary
• Challenges from last Episode
• Archiving
• NSCoder and NSCoding
• Encoding
• Decoding
• Document Architecture
• Info.plist and NSDocumentController
• NSDocument
• Saving and Loading
• More...

Next Time
• Nib files and NSWindowController
• Adding NSPanel to your application
• Adding Preference Panel
• NSBundle
• Challenges
Next Time
• Nib files and NSWindowController
• Adding NSPanel to your application
• Adding Preference Panel
• NSBundle
• Challenges

The Best Code is the Code...
you do not have to Write
The Best Code is the Code...
you do not have to Write

Trackback URL for this post: http://www.cocoacast.com/?q=trackback/27
0
Your rating: None