Last active
August 29, 2015 14:05
-
-
Save akisute/f3b8ed58bd2c35a80458 to your computer and use it in GitHub Desktop.
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
import Foundation | |
import CoreData | |
class Document : NSManagedObject { | |
class func newDocument() -> Document { | |
return self.documentForID("1") | |
} | |
class func documentForID(documentID: String) -> Document { | |
let predicate = NSPredicate(format: "documentID = %@", documentID) | |
return self.findFirstOrInsert(predicate).then(onQueue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {$0 as Document}).wait()! | |
} | |
@NSManaged var documentID:String | |
@NSManaged var notes:[Note] | |
@NSManaged var createdAt:NSDate | |
@NSManaged var updatedAt:NSDate | |
override func awakeFromInsert() { | |
super.awakeFromInsert() | |
self.createdAt = NSDate() | |
self.updatedAt = NSDate() | |
} | |
override func willSave() { | |
super.willSave() | |
let currentTime = NSDate() | |
if currentTime.timeIntervalSinceDate(self.updatedAt) > 1.0 { | |
self.updatedAt = currentTime | |
} | |
} | |
} |
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
extension Promise { | |
func wait(_ timeInterval: NSTimeInterval = 5.0) -> T? { | |
if let v = self.value { | |
return v | |
} | |
var v: T? = nil | |
self.handlers.append({ | |
v = self.value | |
}) | |
NSThread.sleepForTimeInterval(timeInterval) | |
return v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment