Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Session 224 - Using iCloud with NSDocument

wwdcman
June 25, 2012
310

Session 224 - Using iCloud with NSDocument

wwdcman

June 25, 2012
Tweet

Transcript

  1. These are confidential sessions—please refrain from streaming, blogging, or taking

    pictures Session 224 Using iCloud with NSDocument Kevin Perry Software Engineer, Cocoa Frameworks
  2. Provisioning Profile •Identifies your development team •Defines the devices your

    App can run on •Grants iCloud support •Defines your Ubiquity container
  3. Provisioning Profile •Identifies your development team •Defines the devices your

    App can run on •Grants iCloud support •Defines your Ubiquity container •Provisioning Portal
  4. NSSaveOperation User Initiated Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  5. NSSaveOperation My Document User Initiated Save In Place Save Elsewhere

    Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  6. NSSaveOperation My Document My Document User Initiated Save In Place

    Save Elsewhere Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  7. NSSaveToOperation My Document User Initiated Save In Place Save Elsewhere

    Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  8. NSSaveToOperation My Document Another Document User Initiated Save In Place

    Save Elsewhere Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  9. My Document NSSaveAsOperation User Initiated Save In Place Save Elsewhere

    Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  10. My Document NSSaveAsOperation My Document 2 User Initiated Save In

    Place Save Elsewhere Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  11. My Document NSSaveAsOperation My Document 2 User Initiated Save In

    Place Save Elsewhere Save Elsewhere & Redirect “Save” — NSSaveOperation “Export” — NSSaveToOperation “Save As” — NSSaveAsOperation
  12. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation
  13. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveElsewhereOperation
  14. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveElsewhereOperation Untitled
  15. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveElsewhereOperation Autosaved Document Untitled
  16. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveElsewhereOperation Autosaved Document My Document
  17. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveElsewhereOperation My Document
  18. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveInPlaceOperation My Document
  19. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveInPlaceOperation My Document My Document
  20. Auto Save Responsiveness •Asynchronous saving •Cancellable autosaves •See “Auto Save

    and Versions in Mac OS X 10.7 Lion” "Cancellable"  —  matches  spelling  used   in  code  Do  not  correct
  21. Auto Save Responsiveness •Asynchronous saving •Cancellable autosaves •See “Auto Save

    and Versions in Mac OS X 10.7 Lion” ▪ developer.apple.com/videos "Cancellable"  —  matches  spelling  used   in  code  Do  not  correct
  22. Serializing User Activity Print Activity Show UI Perform Present Errors

    Recover Activity Show UI Perform Present Errors Recover
  23. Serializing User Activity Print Conflict Resolution Activity Show UI Perform

    Present Errors Recover Activity Show UI Perform Present Errors Recover
  24. Serializing User Activity Print Conflict Resolution Activity Show UI Perform

    Present Errors Recover Activity Show UI Perform Present Errors Recover
  25. Serializing User Activity Print Conflict Resolution -performActivityWithSynchronousWaiting:usingBlock: Activity Show UI

    Perform Present Errors Recover Activity Show UI Perform Present Errors Recover
  26. -performActivity… -performActivity… Serializing User Activity Print Conflict Resolution -performActivityWithSynchronousWaiting:usingBlock: Completion

    Handler -performActivity… Activity Show UI Perform Present Errors Recover Activity Show UI Perform Present Errors Recover
  27. -performActivity… -performActivity… Serializing User Activity Print Conflict Resolution -performActivityWithSynchronousWaiting:usingBlock: Completion

    Handler -performActivity… Activity Show UI Perform Present Errors Recover Activity Show UI Perform Present Errors Recover
  28. -performActivity… -performActivity… -performActivity… -performActivity… Serializing User Activity Print Conflict Resolution

    -performActivityWithSynchronousWaiting:usingBlock: Activity Show UI Perform Present Errors Recover
  29. -performActivity… -performActivity… -performActivity… -performActivity… Serializing User Activity Print Conflict Resolution

    -performActivityWithSynchronousWaiting:usingBlock: Activity Show UI Perform Present Errors Recover
  30. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    For QC: Colon is part of code—leave in
  31. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    ▪ Invoked synchronously For QC: Colon is part of code—leave in
  32. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    ▪ Invoked synchronously ▪ File access completes on return For QC: Colon is part of code—leave in
  33. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    ▪ Invoked synchronously ▪ File access completes on return ▪ Asynchronous For QC: Colon is part of code—leave in
  34. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    ▪ Invoked synchronously ▪ File access completes on return ▪ Asynchronous ▪ Enqueues block, return immediately For QC: Colon is part of code—leave in
  35. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    ▪ Invoked synchronously ▪ File access completes on return ▪ Asynchronous ▪ Enqueues block, return immediately ▪ Required to work safely with document on non-main thread For QC: Colon is part of code—leave in
  36. File Access •Two types ▪ Synchronous ▪ Blocks main thread

    ▪ Invoked synchronously ▪ File access completes on return ▪ Asynchronous ▪ Enqueues block, return immediately ▪ Required to work safely with document on non-main thread ▪ Resume on main thread using -continueAsynchronousWorkOnMainThreadUsingBlock: For QC: Colon is part of code—leave in
  37. (lldb) bt ... -[NSDocument performSynchronousFileAccessUsingBlock:] -[NSDocument _checkAutosavingThenContinue:] ... (lldb) po

    _NSDocumentSerializationInfo() Pending serialization requests (3 total) [0] FILE ACCESS: Document: "Untitled.sketch2" -- State: ACTIVE ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[SKTDocument doSomethingWithFile] ... [1] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[NSDocument autosaveWithImplicitCancellability:completionHandler:] ! ... [2] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performSynchronousFileAccessUsingBlock:] 2 -[NSDocument _checkAutosavingThenContinue:] ...!
  38. (lldb) bt ... -[NSDocument performSynchronousFileAccessUsingBlock:] -[NSDocument _checkAutosavingThenContinue:] ... (lldb) po

    _NSDocumentSerializationInfo() Pending serialization requests (3 total) [0] FILE ACCESS: Document: "Untitled.sketch2" -- State: ACTIVE ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[SKTDocument doSomethingWithFile] ... [1] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[NSDocument autosaveWithImplicitCancellability:completionHandler:] ! ... [2] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performSynchronousFileAccessUsingBlock:] 2 -[NSDocument _checkAutosavingThenContinue:] ...!
  39. (lldb) bt ... -[NSDocument performSynchronousFileAccessUsingBlock:] -[NSDocument _checkAutosavingThenContinue:] ... (lldb) po

    _NSDocumentSerializationInfo() Pending serialization requests (3 total) [0] FILE ACCESS: Document: "Untitled.sketch2" -- State: ACTIVE ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[SKTDocument doSomethingWithFile] ... [1] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[NSDocument autosaveWithImplicitCancellability:completionHandler:] ! ... [2] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performSynchronousFileAccessUsingBlock:] 2 -[NSDocument _checkAutosavingThenContinue:] ...!
  40. (lldb) bt ... -[NSDocument performSynchronousFileAccessUsingBlock:] -[NSDocument _checkAutosavingThenContinue:] ... (lldb) po

    _NSDocumentSerializationInfo() Pending serialization requests (3 total) [0] FILE ACCESS: Document: "Untitled.sketch2" -- State: ACTIVE ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[SKTDocument doSomethingWithFile] ... [1] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[NSDocument autosaveWithImplicitCancellability:completionHandler:] ! ... [2] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performSynchronousFileAccessUsingBlock:] 2 -[NSDocument _checkAutosavingThenContinue:] ...!
  41. (lldb) bt ... -[NSDocument performSynchronousFileAccessUsingBlock:] -[NSDocument _checkAutosavingThenContinue:] ... (lldb) po

    _NSDocumentSerializationInfo() Pending serialization requests (3 total) [0] FILE ACCESS: Document: "Untitled.sketch2" -- State: ACTIVE ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[SKTDocument doSomethingWithFile] ... [1] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[NSDocument autosaveWithImplicitCancellability:completionHandler:] ! ... [2] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performSynchronousFileAccessUsingBlock:] 2 -[NSDocument _checkAutosavingThenContinue:] ...!
  42. (lldb) bt ... -[NSDocument performSynchronousFileAccessUsingBlock:] -[NSDocument _checkAutosavingThenContinue:] ... (lldb) po

    _NSDocumentSerializationInfo() Pending serialization requests (3 total) [0] FILE ACCESS: Document: "Untitled.sketch2" -- State: ACTIVE ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[SKTDocument doSomethingWithFile] ... [1] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performAsynchronousFileAccessUsingBlock:] ! 2 -[NSDocument autosaveWithImplicitCancellability:completionHandler:] ! ... [2] FILE ACCESS: Document: "Untitled.sketch2" -- State: WAITING ! 1 -[NSDocument performSynchronousFileAccessUsingBlock:] 2 -[NSDocument _checkAutosavingThenContinue:] ...!
  43. - (void)doSomethingWithFile { [self performAsynchronousFileAccessUsingBlock:^(void (^completionHandler)(void)) { if ([self fileURL])

    { [self performSomeFileAccessThenContinue:^{ completionHandler(); }]; } else { completionHandler(); } }]; }
  44. Pre-Save Preservation Versions Backup File Optimization My Document My Document

    -writeSafelyToURL:ofType:forSaveOperation:error: My Document (Backup) My Document
  45. Pre-Save Preservation Versions Backup File Optimization My Document My Document

    -backupFileURL My Document (Backup) -writeSafelyToURL:ofType:forSaveOperation:error: My Document (Backup) My Document
  46. Opening iCloud Documents •App-Centric Open Panel •Non-modal •New API Modal

    Only Non-modal Supported To Invoke To Override -URLsFromRunningOpenPanel -beginOpenPanelWithCompletionHandler: -runModalOpenPanel:forTypes: -beginOpenPanel:forTypes:completionHandler:
  47. Getting Documents In and Out of iCloud •App-Centric Open Panel

    •“Move To…” and “Move to iCloud” For QC: Leave in ellipsis and colon
  48. Getting Documents In and Out of iCloud •App-Centric Open Panel

    •“Move To…” and “Move to iCloud” ▪ NSDocument: -moveToURL:completionHandler: For QC: Leave in ellipsis and colon
  49. Getting Documents In and Out of iCloud •App-Centric Open Panel

    •“Move To…” and “Move to iCloud” ▪ NSDocument: -moveToURL:completionHandler: •Drafts For QC: Leave in ellipsis and colon
  50. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveAsOperation Draft APIs
  51. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveAsOperation Draft APIs 0 Accessors Customize Draft Name Disable Drafts -setDraft: & -isDraft -defaultDraftName +autosavesDrafts
  52. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveAsOperation Draft APIs 0 Accessors Customize Draft Name Disable Drafts -setDraft: & -isDraft -defaultDraftName +autosavesDrafts
  53. User Initiated Automatic Save In Place Save Elsewhere Save Elsewhere

    & Redirect “Save” — NSSaveOperation NSAutosaveInPlaceOperation “Export” — NSSaveToOperation NSAutosaveElsewhereOperation “Save As” — NSSaveAsOperation NSAutosaveAsOperation Draft APIs 0 Accessors Customize Draft Name Disable Drafts -setDraft: & -isDraft -defaultDraftName +autosavesDrafts
  54. Best Practices •Override NSDocument as little as possible •Eliminate open

    panel accessory views •Quick Look thumbnail and preview
  55. Best Practices •Override NSDocument as little as possible •Eliminate open

    panel accessory views •Quick Look thumbnail and preview •File formats
  56. Best Practices •Override NSDocument as little as possible •Eliminate open

    panel accessory views •Quick Look thumbnail and preview •File formats •Consider App running simultaneously on different devices
  57. Best Practices •Override NSDocument as little as possible •Eliminate open

    panel accessory views •Quick Look thumbnail and preview •File formats •Consider App running simultaneously on different devices •Test reverting
  58. Dropping Down a Level •Foundation APIs ▪ File Coordination ▪

    -URLForUbiquityContainerIdentifier: For QC: Colon part of code, leave in.
  59. Dropping Down a Level •Foundation APIs ▪ File Coordination ▪

    -URLForUbiquityContainerIdentifier: ▪ NSMetadataQuery For QC: Colon part of code, leave in.
  60. Dropping Down a Level •Foundation APIs ▪ File Coordination ▪

    -URLForUbiquityContainerIdentifier: ▪ NSMetadataQuery ▪ -ubiquityIdentityToken For QC: Colon part of code, leave in.
  61. Dropping Down a Level •Foundation APIs ▪ File Coordination ▪

    -URLForUbiquityContainerIdentifier: ▪ NSMetadataQuery ▪ -ubiquityIdentityToken ▪ NSUbiquityIdentityDidChangeNotification For QC: Colon part of code, leave in.
  62. Dropping Down a Level •Foundation APIs ▪ File Coordination ▪

    -URLForUbiquityContainerIdentifier: ▪ NSMetadataQuery ▪ -ubiquityIdentityToken ▪ NSUbiquityIdentityDidChangeNotification •+usesUbiquitousStorage ▪ Disable NSDocument iCloud features For QC: Colon part of code, leave in.
  63. Related Sessions Using iCloud with UIDocument Marina Wednesday 10:15AM Advanced

    iCloud Document Storage Marina Thursday 3:15PM Using iCloud with Core Data Mission Thursday 4:30PM
  64. Open and Save Panels Within an App Sandbox Q &

    A Lab Essentials Lab B Wednesday 4:30PM Cocoa and OS X Gestures Lab Essentials Lab B Thursday 2:00PM iCloud Storage Lab Essentials Lab B Thursday 4:30PM Labs iCloud Storage Lab Essentials Lab B Friday 11:30AM Cocoa and XPC Lab Essentials Lab B Friday 10:30AM Security Lab Core OS Lab B Thursday 9:00AM