Pro Yearly is on sale from $80 to $50! »

Core Data to UIDocument for iCloud

Core Data to UIDocument for iCloud

These slides are from a talk I did for CocoaHeads Belgium. I talked about how I moved my “B-list” shopping list app for iOS from Core Data to UIDocument for syncing lists with iCloud.

Cdaee8530af3550f5f3172805754be65?s=128

Steven Vandeweghe

April 30, 2013
Tweet

Transcript

  1. iCloud It Just Works®

  2. B-list structure

  3. Current Version • Uses Core Data • No sync •

    No history
  4. Model

  5. • Key/Value • Core Data • Documents

  6. Key/Value + - works well limited to 1MB not a

    good fit
  7. Core Data + - good fit It Just Works, Sometimes®

  8. Documents + - Like Dropbox not a good fit (for

    my app)
  9. OS X System Preferences.app > iCloud > Manage...

  10. Finder

  11. iOS Settings.app > iCloud > Storage & Backup > Manage

    Storage
  12. Documents folder Create a /Documents folder in the iCloud container.

    I you don’t store files in /Documents, you won’t see them individually.
  13. UIDocument

  14. UIDocument has a URL - (id)initWithFileURL:(NSURL *)url

  15. UIDocument can read and write - (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError

    **)outError - (id)contentsForType:(NSString *)typeName error:(NSError **)outError id: NSData or NSFileWrapper
  16. UIDocument has a life cycle - (void)saveToURL:(NSURL *)url forSaveOperation: (UIDocumentSaveOperation)saveOperation

    completionHandler:(void (^)(BOOL success))completionHandler - (void)openWithCompletionHandler:(void (^)(BOOL success))completionHandler - (void)closeWithCompletionHandler:(void (^)(BOOL success))completionHandler
  17. None
  18. UIDocument has autosave @property(retain) NSUndoManager *undoManager - (void)updateChangeCount:(UIDocumentChangeKind)change

  19. About that URL... it can be local documentURL = [NSURL

    fileURLWithPath:NSHomeDirectory()]; or in iCloud documentURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; → don’t call this on the main thread!
  20. Local or iCloud?

  21. Why in Settings?

  22. Is this thing on? dispatch_async(dispatch_get_global_queue(...), ^{ NSFileManager *fm = [NSFileManager

    defaultManager]; ! ! url = [fm URLForUbiquityContainerIdentifier:nil]; }); iOS 6 NSFileManager *fm = [NSFileManager defaultManager]; id token = [fm ubiquityIdentityToken]; → you can call this on the main thread
  23. Find out about updates

  24. NSMetadataQuery _query = [[NSMetadataQuery alloc] init]; _query.searchScopes = @[NSMetadataQueryUbiquitousDocumentsScope]; _query.predicate

    = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.blist'", NSMetadataItemFSNameKey]; [_query setNotificationBatchingInterval:0.5]; [_query startQuery]; → NSMetadataQueryDidFinishGatheringNotification → NSMetadataQueryDidUpdateNotification [[_query resultAtIndex:index] valueForAttribute:NSMetadataItemURLKey]; [_query disableUpdates]; [_query enableUpdates];
  25. UIDocument → UIDocumentStateChangedNotification UIDocumentState state = doc.documentState; UIDocumentStateNormal UIDocumentStateInConflict UIDocumentStateEditingDisabled

    UIDocumentStateSavingError
  26. Conflicts NSFileVersion *currentVersion = [NSFileVersion currentVersionOfItemAtURL:doc.fileURL]; NSFileVersion NSArray *conflictVersions =

    [[NSFileVersion unresolvedConflictVersionsOfItemAtURL:…] + (BOOL)removeOtherVersionsOfItemAtURL:(NSURL *)inFileURL error:(NSError **)outError @property (getter=isResolved) BOOL resolved @property(readonly) NSURL *URL
  27. Coordinated file operations NSFileCoordinator [fc coordinateWritingItemAtURL:documentURL ! ! ! !

    ! ! ! options:NSFileCoordinatorWritingForDeleting ! ! ! ! ! ! ! error:&error ! ! ! ! ! ! ! byAccessor:^(NSURL *newURL) { ! NSError *e; ! NSFileManager *fm = [[NSFileManager alloc] init]; ! BOOL success = [fm removeItemAtURL:newURL error:&e]; ! if (!success) { ! ! ... ! } }];
  28. Demo

  29. The End Steven Vandeweghe @bluecrowbar steven@bluecrowbar.com