 import UIKit
 
 
+private func async_main_safe(block: () -> Void) {
+    
+    if NSThread.isMainThread() {
+        block()
+    } else {
+        dispatch_async(dispatch_get_main_queue(), block)
+    }
+}
+
 protocol TableElementProtocol: NSObjectProtocol {
 
     class var identifier: String { get }

    }
    
    public func invalidate() {

        dispatch_async(dispatch_get_main_queue()) {
            self.updateWithoutAnimate()
        }
    }


    func addTransaction(t: (Processor, NSKeyValueChange, Int)) {

        async_main_safe {
            if self.displayLink == nil {
                self.displayLink = CADisplayLink(target: self, selector: "update")
                self.displayLink?.frameInterval = 20
