     
 }
 
-// MARK: Attribute predicate support
+// MARK: - Attribure - create, delete and refresh entities
+
+extension Table {
+
+    /// Try to find the first entity matching the comparison. If the entity does not exist a new one will be created.
+    ///
+    /// :param: predicateClosure A closure with a simple equality comparison between an attribute and a value.
+    ///
+    /// :returns: The found entity or a new entity from the same type (with the attribute filled with the specified value).
+    public func firstOrCreated(predicateClosure: (T.Type) -> NSComparisonPredicate) -> T {
+        let predicate = predicateClosure(T.self)
+        if let entity = self.filterBy(predicate: predicate).first() {
+            return entity
+        }
+        else {
+            let entity = self.createEntity()
+
+            let attributeName = predicate.leftExpression.keyPath
+            let value: AnyObject = predicate.rightExpression.constantValue
+            
+            entity.setValue(value, forKey: attributeName)
+            
+            return entity
+        }
+    }
+    
+}
+
+
+// MARK: - Attribute - predicate support
 
 extension Table {
     

extension Table {
    
    public func createEntity() -> T {
        let managedObject = T(entity: self.entityDescription, insertIntoManagedObjectContext: self.context.managedObjectContext)
        
        return managedObject
    }
    
    public func createOrGetFirstEntity(whereAttribute attributeName: String, isEqualTo value: AnyObject?) -> T {
        if let entity = self.filterBy(attribute: attributeName, value: value).first() {
            return entity
        }

    
}

// MARK: - Attribute - ordering support

extension Table {
    
