Created
April 2, 2015 18:04
-
-
Save juliantejera/f30a09e36182b33d355d to your computer and use it in GitHub Desktop.
ManagedDocumentCoordinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ManagedDocumentHandler.swift | |
// DominicanFuel | |
// | |
// Created by Julian Tejera on 3/31/15. | |
// Copyright (c) 2015 Julian Tejera. All rights reserved. | |
// | |
import UIKit | |
protocol ManagedDocumentCoordinatorDelegate: class { | |
func managedDocumentCoordinator(coordinator: ManagedDocumentCoordinator, didOpenDocument document: UIManagedDocument) | |
func managedDocumentCoordinator(coordinator: ManagedDocumentCoordinator, didFailWithError error: NSError) | |
} | |
class ManagedDocumentCoordinator { | |
var documentURL: NSURL! | |
var document: UIManagedDocument? | |
weak var delegate: ManagedDocumentCoordinatorDelegate? | |
init() { | |
} | |
func setupDocument(documentName: String) { | |
if let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as? NSURL { | |
self.documentURL = documentsDirectory.URLByAppendingPathComponent(documentName) | |
self.document = UIManagedDocument(fileURL: documentURL) | |
open() | |
} | |
} | |
private func open() { | |
if let path = self.documentURL.path { | |
if NSFileManager.defaultManager().fileExistsAtPath(path) { | |
document?.openWithCompletionHandler(openOrCreateCompletionHandler) | |
} else { | |
document?.saveToURL(self.documentURL, forSaveOperation: UIDocumentSaveOperation.ForCreating, completionHandler: openOrCreateCompletionHandler) | |
} | |
} | |
} | |
private func openOrCreateCompletionHandler(success: Bool) { | |
if success { | |
documentIsReady() | |
} else { | |
let userInfo = [NSLocalizedDescriptionKey: "Couldn't create document at: \(self.documentURL)"] | |
let error = NSError(domain: "ManagedDocumentCoordinator", code: 100, userInfo: userInfo) | |
delegate?.managedDocumentCoordinator(self, didFailWithError: error) | |
} | |
} | |
private func documentIsReady() { | |
if self.document?.documentState == UIDocumentState.Normal { | |
delegate?.managedDocumentCoordinator(self, didOpenDocument: self.document!) | |
} else { | |
// Try to open it again | |
open() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment