Auto Save and Versions in Mac OS X 10.7 Lion - Save and Versions in Mac OS X 10.7 ... File...

Click here to load reader

  • date post

    14-May-2018
  • Category

    Documents

  • view

    224
  • download

    0

Embed Size (px)

Transcript of Auto Save and Versions in Mac OS X 10.7 Lion - Save and Versions in Mac OS X 10.7 ... File...

  • These are confidential sessionsplease refrain from streaming, blogging, or taking pictures

    Session 107

    Auto Save and Versions in Mac OS X 10.7 Lion

    Tony ParkerSoftware Engineer, Cocoa Frameworks

    1

  • Agenda

    iCloudVersionsAuto Save

    2

  • NSDocument

    3

  • Document-Based Application Basics

    4

  • Document-Based Applications

    5

  • NSDocument Features

    6

  • NSDocument Features

    7

  • NSDocument Features

    Edited state with undo integration Safe save Error presentation

    8

  • NSDocument Features

    Sandboxing support File coordination Duplication Auto Save Versions Cloud document storage

    9

  • Document Application Architecture

    NSDocumentController NSWindowController NSDocument

    10

  • NSDocumentController

    Create, open, track, and manage documents+ (id)sharedDocumentController;

    - (NSArray *)documents;

    - (IBAction)newDocument:(id)sender;

    - (void)openDocumentWithContentsOfURL:(NSURL *)url display:(BOOL)displayDocument completionHandler:(void (^)(NSDocument *document, BOOL documentWasOpen, NSError *error))handler;

    11

  • NSWindowController

    Manage one window associated with a document Typically customized by subclassing- (id)initWithWindowNibName:(NSString *)windowNibName owner:(id)owner;

    - (id)document;

    12

  • NSDocument

    Represent, store, and load persistent data Always subclassed- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError;

    - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError;

    - (IBAction)saveDocument:(id)sender;

    - (NSURL *)fileURL;

    - (BOOL)isDocumentEdited;

    13

  • NSDocumentController

    MyRTFDocument

    MyRTFWindowController

    MyPlainTextDocument

    MyWindowController MyWindowController

    Document Application Architecture

    14

  • -openDocument:

    -openDocumentWithContentsOfURL:display:error:

    -typeForContentsOfURL:error:

    -makeDocumentWithContentsOfURL:ofType:error:

    NSD

    ocumentController

    -URLsFromRunningOpenPanel:

    MyRTFD

    ocument

    Opening a Document

    15

  • -initWithContentsOfURL:ofType:error:

    -addDocument:

    -makeWindowControllers:

    -showWindows:

    -showWindow:

    NSD

    ocumentController

    -readFromURL:ofType:error:

    -readFromData:ofType:error:MyRTFD

    ocument

    MyRTFW

    indowController

    -initWithWindowNibName: owner:

    -readFromFileWrapper:ofType:error:or

    16

  • -saveDocument:

    -saveToURL:ofType:forSaveOperation:completionHandler:

    -writeSafelyToURL:ofType:forSaveOperation:error:

    -writeToURL:ofType:forSaveOperation:originalContentsURL:error:

    -writeToURL:ofType:error:

    -dataOfType:error:

    More custom

    More for free-fileWrapperOfType:error:

    MyRTFD

    ocument

    or

    Saving a Document

    17

  • Auto Save

    18

  • 19

  • Auto Save

    Saves automatically at key times Frees user from worry of data loss Relieves user of manual and repetitive task

    Document on disk is document on screen

    20

  • Enabling Auto Save

    @implementation MyDocument

    + (BOOL)autosavesInPlace { return YES; }

    @end

    21

  • Demo

    Kevin PerrySoftware Engineer, Cocoa Frameworks

    22

  • Polishing Auto Save User Experience

    Be smart about when to save Allow the user to cancel a save Save on background threads

    23

  • Polishing Auto Save User ExperienceBe smart about when to save

    NSDocument triggers autosave Quit Document close Switching applications Timer Reverting Your application can also trigger autosave

    24

  • Taking Advantage of File Coordination Pacific HeightsTuesday 4:30PM

    File CoordinationSaving 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

    25

  • Polishing Auto Save User ExperienceSpend less time saving

    Saves now happen more frequently Avoid interrupting the user Only save whats changed

    Use NSFileWrapper Resources are stored in natural format Allows quick copies

    26

  • Polishing Auto Save User ExperienceAvoid interrupting the user

    Two kinds of autosaving Normal

    Application quit Document close Too long since last save

    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 ExperienceEnable asynchronous saving

    @implementation MyDocument

    - (BOOL)canAsynchronouslyWriteToURL:(NSURL *)url ofType:(NSString *)type forSaveOperation:(NSSaveOperationType)op { return YES; }

    @end

    29

  • Edit

    Timer Prepare Write Finish

    EditWait

    now!

    Synchronous Autosaving

    30

  • Asynchronous Autosaving

    Edit

    Timer Prepare Write Finish

    EditWait

    now!

    31

  • Asynchronous SavingUse background threads

    Save operation starts on main thread-saveToURL:ofType:forSaveOperation:completionHandler:

    Main thread blocks Saving started on background thread-writeSafelyToURL:ofType:forSaveOperation:error:

    Background thread allows main thread to continue after snapshot-unblockUserInteraction

    Background thread finishes saving

    32

  • Serialized Resources

    User Activities

    SaveRevert

    DuplicatePrint

    File Access

    URLModification dateOn-disk contents

    Edit state

    33

  • Asynchronous SavingCoordinating threads during save

    Main thread needs to present something to the user

    - (void)performActivityWithSynchronousWaiting:(BOOL)waitSynchronously usingBlock: (void (^)(void (^activityCompletionHandler)(void)))block;

    Any thread needs to access the document file

    - (void)performSynchronousFileAccessUsingBlock:(void (^)(void))block;

    - (void)performAsynchronousFileAccessUsingBlock: (void (^)(void (^fileAccessCompletionHandler)(void)))block;

    34

  • Asynchronous File Access Example

    main thread

    Save completion handler

    Perform background workEnd asynchronous file access

    Start background operationAsynchronous file access

    35

  • Asynchronous SavingAvoiding 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

    Save completion handler

    Calculate file size

    Start background operationAsynchronous file access

    Run on main thread

    Duplicate

    Synchronous file access

    37

  • Calculate File Size

    main thread

    Save completion handler

    Calculate file size

    Start background operationAsynchronous file access

    Continue asynchronous work on main thread

    Duplicate

    Synchronous file access

    End asynchronous file access

    Update user interface

    Finish duplicateEnd synchronous file access

    38

  • Demo

    Kevin PerrySoftware Engineer, Cocoa Frameworks

    39

  • Time to user responsivenessPerformance Gains from Unblocking Early

    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.

    Synchronous

    Asynchronous

    Copy and Unblock

    .676 sec

    .649 sec

    .011 sec

    40

  • Versions

    41

  • 42

  • Versions Browser

    Expands revert options Further back in time Selective reverting

    Protects against unintentional change

    Compressed storage Automatically manages versions Integrated with Time Machine

    43

  • Polishing Versions User Experience

    Adopt asynchronous document opening Show only relevant controls in Versions browser Resize windows to best viewing dimensions Allow discardable changes

    44

  • Polishing Versions User ExperienceShow different interface for versions

    Provide focused interface, including hiding toolbars and inspectors Notifications are posted on version browser enter and exitNSWindowWillEnterVersionBrowserNotification

    NSWindowDidExitVersionBrowserNotification

    45

  • Show different interface for versions

    Document displayed in version browser-(BOOL)isInViewingMode;

    Polishing Versions User Experience

    46

  • Polishing Versions User ExperienceMake document window the best size

    NSWindowDele