     }
 
     private func rollback() {
+        guard realm.isInWriteTransaction else { return }
         realm.cancelWrite()
         metadata = [:]
     }


    public var transaction: TorchTransactionHelper {
        return TorchTransactionHelper(
            begin: realm.beginWrite,
            commit: { self.commit(errorHandler: self.defaultOnWriteError) },
            commitWithErrorHandler: commit(errorHandler:),
            rollback: rollback)

        realm.beginWrite()
        closure(rollback)
        // In case we rolled back
        commit(errorHandler: onWriteError)
        return self
    }

    private func begin() {
        guard !realm.isInWriteTransaction else { return }
        realm.beginWrite()
    }

    private func commit(errorHandler: OnWriteErrorListener) {
        guard realm.isInWriteTransaction else { return }
        do {
            try realm.commitWrite()
        } catch {
