<commit_before>//
//  ER_Delete.swift
//  Pods
//
//  Created by <NAME> on 23/11/16.
//
//

import UIKit
import Realm
import RealmSwift

public enum EasyRealmDeleteMethod {
  case normal
  case cascade
}

public extension EasyRealmStatic where T:Object {
  
  public func deleteAll(with method:EasyRealmDeleteMethod = .normal) throws {
    let realm = try Realm()
    try realm.write {
      realm.delete(realm.objects(self.baseType))
    }
  }
  
}

public extension EasyRealm where T:Object {
  
  public func delete(with method:EasyRealmDeleteMethod = .normal) throws {
    switch method {
    case .normal:     self.isManaged ? try managedDelete() : try unmanagedDelete()
    case .cascade:    self.isManaged ? try managedCascadeDelete() : try unmanagedCascadeDelete()
    }
  }
  
}



//Normal Way
fileprivate extension EasyRealm where T: Object {
  
  fileprivate func managedDelete() throws {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    let ref = ThreadSafeReference(to: self.base)
    try rq.queue.sync {
      guard let object = rq.realm.resolve(ref) else { throw EasyRealmError.ObjectCantBeResolved }
      try rq.realm.write {
        rq.realm.delete(object)
      }
    }
  }
  
  fileprivate func unmanagedDelete() throws  {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    guard let key = T.primaryKey() else { throw EasyRealmError.ObjectCantBeResolved }
  
    try rq.queue.sync {
      let value = self.base.value(forKey: key)
      if let object = rq.realm.object(ofType: T.self, forPrimaryKey: value) {
        try rq.realm.write {
          rq.realm.delete(object)
        }
      }
    }
  }
  
  
}

//Normal Way
fileprivate extension EasyRealm where T: Object {
  
  fileprivate func managedCascadeDelete() throws {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    let ref = ThreadSafeReference(to: self.base)
    try rq.queue.sync {
      guard let object = rq.realm.resolve(ref) else { throw EasyRealmError.ObjectCantBeResolved }
      try rq.realm.write {
        for property in object.objectSchema.properties {
          guard let value = object.value(forKey: property.name) else { continue }
          if let object = value as? Object {
            try object.er.delete(with: .cascade)
          } else if let object = value as? EasyRealmList  {
            try object.children().forEach { try $0.er.delete(with: .cascade) }
          }
        }
        rq.realm.delete(object)
      }
    }
  }
  
  fileprivate func unmanagedCascadeDelete() throws  {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    guard let key = T.primaryKey() else { throw EasyRealmError.ObjectCantBeResolved }
    
    try rq.queue.sync {
      let value = self.base.value(forKey: key)
      if let object = rq.realm.object(ofType: T.self, forPrimaryKey: value) {
        try rq.realm.write {
          for property in object.objectSchema.properties {
            guard let value = object.value(forKey: property.name) else { continue }
            if let object = value as? Object {
              try object.er.delete(with: .cascade)
            } else if let object = value as? EasyRealmList  {
              try object.children().forEach { try $0.er.delete(with: .cascade) }
            }
          }
          rq.realm.delete(object)
        }
      }
    }
  }
  
}
<commit_msg>seconde version of Cascade Delete<commit_after>//
//  ER_Delete.swift
//  Pods
//
//  Created by <NAME> on 23/11/16.
//
//

import UIKit
import Realm
import RealmSwift

public enum EasyRealmDeleteMethod {
  case normal
  case cascade
}

public extension EasyRealmStatic where T:Object {
  
  public func deleteAll(with method:EasyRealmDeleteMethod = .normal) throws {
    let realm = try Realm()
    try realm.write {
      realm.delete(realm.objects(self.baseType))
    }
  }
  
}

public extension EasyRealm where T:Object {
  
  public func delete(with method:EasyRealmDeleteMethod = .normal) throws {
    switch method {
    case .normal:     self.isManaged ? try managedNormalDelete() : try unmanagedNormalDelete()
    case .cascade:    self.isManaged ? try managedCascadeDelete() : try unmanagedCascadeDelete()
    }
  }
  
}



//Normal Way
fileprivate extension EasyRealm where T: Object {
  
  fileprivate func managedNormalDelete() throws {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    let ref = ThreadSafeReference(to: self.base)
    try rq.queue.sync {
      guard let object = rq.realm.resolve(ref) else { throw EasyRealmError.ObjectCantBeResolved }
      try rq.realm.write {
        EasyRealm.normalDelete(this: object, in: rq)
      }
    }
  }
  
  fileprivate func unmanagedNormalDelete() throws  {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    guard let key = T.primaryKey() else { throw EasyRealmError.ObjectCantBeResolved }
    
    try rq.queue.sync {
      let value = self.base.value(forKey: key)
      if let object = rq.realm.object(ofType: T.self, forPrimaryKey: value) {
        try rq.realm.write {
          EasyRealm.normalDelete(this: object, in: rq)
        }
      }
    }
  }
  
  fileprivate static func normalDelete(this object:Object, in queue:EasyRealmQueue) {
    queue.realm.delete(object)
  }
  
  
}

//Cascade Way
fileprivate extension EasyRealm where T: Object {
  
  fileprivate func managedCascadeDelete() throws {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    let ref = ThreadSafeReference(to: self.base)
    try rq.queue.sync {
      guard let object = rq.realm.resolve(ref) else { throw EasyRealmError.ObjectCantBeResolved }
      try rq.realm.write {
        EasyRealm.cascadeDelete(this: object, in: rq)
      }
    }
  }
  
  fileprivate func unmanagedCascadeDelete() throws  {
    guard let rq = EasyRealmQueue.shared else { throw EasyRealmError.RealmQueueCantBeCreate }
    guard let key = T.primaryKey() else { throw EasyRealmError.ObjectCantBeResolved }
    
    try rq.queue.sync {
      let value = self.base.value(forKey: key)
      if let object = rq.realm.object(ofType: T.self, forPrimaryKey: value) {
        try rq.realm.write {
          EasyRealm.cascadeDelete(this: object, in: rq)
        }
      }
    }
  }
  
  
  fileprivate static func cascadeDelete(this object:Object, in queue:EasyRealmQueue) {
    for property in object.objectSchema.properties {
      guard let value = object.value(forKey: property.name) else { continue }
      if let object = value as? Object {
        EasyRealm.cascadeDelete(this: object, in: queue)
      }
    }
    queue.realm.delete(object)
  }
  
}

{'fraction_non_alphanumeric': 0.09149749926448955, 'fraction_numerical': 0.002059429243895263, 'mean_word_length': 2.7031590413943354, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4096883', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2156, 'n_words': 525}