Beruflich Dokumente
Kultur Dokumente
10.7 Lion
Session 107
Tony Parker
Software Engineer, Cocoa Frameworks
These are confidential sessionsplease refrain from streaming, blogging, or taking pictures
1
Agenda
2
NSDocument
3
Document-Based Application Basics
4
Document-Based Applications
5
NSDocument Features
6
NSDocument Features
7
NSDocument Features
8
NSDocument Features
Sandboxing support
File coordination
Duplication
Auto Save
Versions
Cloud document storage
9
Document Application Architecture
NSDocumentController
NSWindowController
NSDocument
10
NSDocumentController
11
NSWindowController
- (id)document;
12
NSDocument
13
Document Application Architecture
NSDocumentController
MyRTFDocument MyPlainTextDocument
14
Opening a Document
-openDocument:
NSDocumentController
-URLsFromRunningOpenPanel:
-openDocumentWithContentsOfURL:display:error:
-typeForContentsOfURL:error:
-makeDocumentWithContentsOfURL:ofType:error:
MyRTFDocument
15
-initWithContentsOfURL:ofType:error:
-readFromURL:ofType:error:
-readFromFileWrapper:ofType:error:
or
NSDocumentController
-readFromData:ofType:error:
MyRTFDocument
-addDocument:
-makeWindowControllers:
-initWithWindowNibName:
MyRTFWindowController
owner:
-showWindows:
-showWindow:
16
Saving a Document
-saveDocument:
MyRTFDocument
More custom
-saveToURL:ofType:forSaveOperation:completionHandler:
-writeSafelyToURL:ofType:forSaveOperation:error:
-writeToURL:ofType:forSaveOperation:originalContentsURL:error:
-writeToURL:ofType:error:
-dataOfType:error: or -fileWrapperOfType:error:
More for free
17
Auto Save
18
19
Auto Save
20
Enabling Auto Save
@implementation MyDocument
+ (BOOL)autosavesInPlace { return YES; }
@end
21
Demo
Kevin Perry
Software Engineer, Cocoa Frameworks
22
Polishing Auto Save User Experience
23
Polishing Auto Save User Experience
Be smart about when to save
NSDocument triggers autosave
Quit
Document close
Switching applications
Timer
Reverting
Your application can also trigger autosave
24
File Coordination
Saving and loading on demand
Serializes access to files between applications
NSDocument implements it for you
Autosaves when read is requested
Notifies when write is completed
Pacific Heights
Taking Advantage of File Coordination Tuesday 4:30PM
25
Polishing Auto Save User Experience
Spend less time saving
Saves now happen more frequently
Avoid interrupting the user
Only save whats changed
Use NSFileWrapper
Resources are stored in natural format
26
Polishing Auto Save User Experience
Avoid interrupting the user
Two kinds of autosaving
Normal
Application quit
Document close
Implicitly cancellable
Timer
27
Canceling Saving
- (BOOL)writeToURL:(NSURL *)url
ofType:(NSString *)type
error:(NSError **)err
{
while (stillSaving) {
// Do some work
if ([self autosavingIsImplicitlyCancellable] && checkForUserActivity()) {
// Abort save
if (err) *err = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSUserCancelledError
userInfo:nil];
return NO;
}
}
}
28
Polishing Auto Save User Experience
Enable asynchronous saving
@implementation MyDocument
- (BOOL)canAsynchronouslyWriteToURL:(NSURL *)url
ofType:(NSString *)type
forSaveOperation:(NSSaveOperationType)op
{
return YES;
}
@end
29
Synchronous Autosaving
30
Asynchronous Autosaving
Timer now!
Prepare Write Finish
31
Asynchronous Saving
Use background threads
Save operation starts on main thread
-saveToURL:ofType:forSaveOperation:completionHandler:
32
Serialized Resources
Save URL
Revert Modification date
Duplicate On-disk contents
Print Edit state
33
Asynchronous Saving
Coordinating threads during save
Main thread needs to present something to the user
- (void)performActivityWithSynchronousWaiting:(BOOL)waitSynchronously
usingBlock:
(void (^)(void (^activityCompletionHandler)(void)))block;
- (void)performAsynchronousFileAccessUsingBlock:
(void (^)(void (^fileAccessCompletionHandler)(void)))block;
34
Asynchronous File Access Example
main thread
Start background operation
Asynchronous file access
Perform background work
End asynchronous file access
35
Asynchronous Saving
Avoiding deadlock
User activities and file access must be coordinated
Use NSDocument methods
- (void)continueAsynchronousWorkOnMainThreadUsingBlock:(void (^)(void))block;
- (void)continueActivityUsingBlock:(void (^)(void))block;
Example
After save, calculate file size
Display to user in inspector panel
36
Calculate File Size
main thread
Start background operation
Asynchronous file access
Duplicate
Calculate file size
Synchronous file access
Run on main thread
37
Calculate File Size
main thread
Start background operation
Asynchronous file access
Duplicate
Calculate file size
Synchronous file access
Continue asynchronous work
on main thread
Update user interface
38
Demo
Kevin Perry
Software Engineer, Cocoa Frameworks
39
Performance Gains from Unblocking Early
Time to user responsiveness
Time the main thread spends blocked during autosave of Sketch document with 10,000 shapes. MacBook Pro, 2.8 GHz Core 2 Duo, 4 GB RAM.
40
Versions
41
42
Versions Browser
43
Polishing Versions User Experience
44
Polishing Versions User Experience
Show different interface for versions
Provide focused interface, including hiding toolbars and inspectors
Notifications are posted on version browser enter and exit
NSWindowWillEnterVersionBrowserNotification
NSWindowDidExitVersionBrowserNotification
45
Polishing Versions User Experience
Show different interface for versions
Document displayed in version browser
-(BOOL)isInViewingMode;
46
Polishing Versions User Experience
Make document window the best size
NSWindowDelegate method
- (NSSize)window:(NSWindow *)window
willResizeForVersionBrowserWithMaxPreferredSize:(NSSize)prefSize
maxAllowedSize:(NSSize)allowedSize;
47
Polishing Versions User Experience
Allow some changes to viewing mode documents
Changes to viewing mode documents are automatically undone
To allow, the change must be discardable
Normally autosaved
May be discarded on locked or viewing mode documents
Examples
Expanded or hidden state of slide group in Keynote
48
Polishing Versions User Experience
Allow some changes to viewing mode versions
Use via NSUndoManager
[myUndoManager setActionIsDiscardable:YES];
Or via NSDocument
[myDocument updateChangeCount:
NSChangeDone | NSChangeDiscardable];
49
NSFileVersion
50
NSFileVersion
51
NSFileVersion
Properties of an NSFileVersion
URL
Name and modification date
Discardability
52
Demo
Kevin Perry
Software Engineer, Cocoa Frameworks
53
iCloud
54
Cloud Storage
55
Moving a Document to iCloud
Use NSFileManager
Find container
- (NSURL *)URLForUbiquityContainerIdentifier:(NSString *)identifier;
56
Finding iCloud Documents
Use NSMetadataQuery
New search scope
NSMetadataQueryUbiquitousDocumentsScope
NSURL properties
NSURLIsUbiquitousItemKey
NSURLUbiquitousItemIsDownloadedKey / NSURLUbiquitousItemIsDownloadingKey
NSURLUbiquitousItemPercentDownloadedKey
NSURLUbiquitousItemIsUploadedKey / NSURLUbiquitousItemIsUploadingKey
NSURLUbiquitousItemPercentUploadedKey
57
Demo
Kevin Perry
Software Engineer, Cocoa Frameworks
58
What We Learned
Make your document-based app a first-class Lion citizen
Auto Save
+ (BOOL)autosavesInPlace { return YES; }
Polish by saving fast and adopting async saving
Versions
Polish by streamlining interface in browsing mode and allowing
discardable changes
iCloud
Moving documents to cloud, finding documents in cloud
Cloud document properties
59
More Information
Bill Dudney
Application Frameworks Evangelist
dudney@apple.com
Documentation
Mac OS X Dev Center
http://developer.apple.com/devcenter/mac
60
Related Sessions
Pacific Heights
Taking Advantage of File Coordination Tuesday 4:30PM
Marina
Resume and Automatic Termination in Lion Thursday 9:00AM
61
Labs
62
63