<commit_before>//
//  RealmExtension.swift
//  Rocket.Chat
//
//  Created by <NAME> on 7/18/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import Foundation
import SwiftyJSON
import RealmSwift

var realmConfiguration: Realm.Configuration?

extension Realm {

    static var current: Realm? {
        if let configuration = realmConfiguration {
            return try? Realm(configuration: configuration)
        } else {
            let configuration = Realm.Configuration(
                deleteRealmIfMigrationNeeded: true
            )

            return try? Realm(configuration: configuration)
        }
    }

    static let writeQueue = DispatchQueue(label: "chat.rocket.realm.write", qos: .background)

    func execute(_ execution: @escaping (Realm) -> Void, completion: VoidCompletion? = nil) {
        var backgroundTaskId: UIBackgroundTaskIdentifier?

        backgroundTaskId = UIApplication.shared.beginBackgroundTask(withName: "chat.rocket.realm.background", expirationHandler: {
            backgroundTaskId = UIBackgroundTaskInvalid
        })

        if let backgroundTaskId = backgroundTaskId {
            let config = self.configuration

            Realm.writeQueue.async {
                if let realm = try? Realm(configuration: config) {
                    try? realm.write {
                        execution(realm)
                    }
                }

                DispatchQueue.main.async {
                    completion?()
                }

                UIApplication.shared.endBackgroundTask(backgroundTaskId)
            }
        }
    }

    static func execute(_ execution: @escaping (Realm) -> Void, completion: VoidCompletion? = nil) {
        Realm.current?.execute(execution, completion: completion)
    }

    static func executeOnMainThread(_ execution: @escaping (Realm) -> Void) {
        guard let realm = self.current else { return }

        try? realm.write {
            execution(realm)
        }
    }

    // MARK: Mutate

    // This method will add or update a Realm's object.
    static func delete(_ object: Object) {
        guard !object.isInvalidated else { return }

        self.execute({ realm in
            realm.delete(object)
        })
    }

    // This method will add or update a Realm's object.<commit_msg>Refactor Realm.executeOnMainThread to add the possibility of giving a specific instance to execute<commit_after>//
//  RealmExtension.swift
//  Rocket.Chat
//
//  Created by <NAME> on 7/18/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import Foundation
import SwiftyJSON
import RealmSwift

var realmConfiguration: Realm.Configuration?

extension Realm {

    static var current: Realm? {
        if let configuration = realmConfiguration {
            return try? Realm(configuration: configuration)
        } else {
            let configuration = Realm.Configuration(
                deleteRealmIfMigrationNeeded: true
            )

            return try? Realm(configuration: configuration)
        }
    }

    static let writeQueue = DispatchQueue(label: "chat.rocket.realm.write", qos: .background)

    func execute(_ execution: @escaping (Realm) -> Void, completion: VoidCompletion? = nil) {
        var backgroundTaskId: UIBackgroundTaskIdentifier?

        backgroundTaskId = UIApplication.shared.beginBackgroundTask(withName: "chat.rocket.realm.background", expirationHandler: {
            backgroundTaskId = UIBackgroundTaskInvalid
        })

        if let backgroundTaskId = backgroundTaskId {
            let config = self.configuration

            Realm.writeQueue.async {
                if let realm = try? Realm(configuration: config) {
                    try? realm.write {
                        execution(realm)
                    }
                }

                DispatchQueue.main.async {
                    completion?()
                }

                UIApplication.shared.endBackgroundTask(backgroundTaskId)
            }
        }
    }

    static func execute(_ execution: @escaping (Realm) -> Void, completion: VoidCompletion? = nil) {
        Realm.current?.execute(execution, completion: completion)
    }

    static func executeOnMainThread(realm: Realm? = nil, _ execution: @escaping (Realm) -> Void) {
        if let realm = realm {
            try? realm.write {
                execution(realm)
            }

            return
        }

        guard let currentRealm = Realm.current else { return }

        try? currentRealm.write {
            execution(currentRealm)
        }
    }

    // MARK: Mutate

    // This method will add or update a Realm's object.
    static func delete(_ object: Object) {
        guard !object.isInvalidated else { return }

        self.execute({ realm in
            realm.delete(object)
        })
    }

    // This method will add or update a Realm's object.
{'fraction_non_alphanumeric': 0.08483606557377049, 'fraction_numerical': 0.0036885245901639345, 'mean_word_length': 1.9725943970767357, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '2482925', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1226, 'n_words': 331}