Index: permissions_by_term.module
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- permissions_by_term.module	(revision c44a5e4143fe5b22f7eede58c7f6acb6bfd8e6fd)
+++ permissions_by_term.module	(revision )
@@ -85,13 +85,29 @@
   $termId = $formState->getFormObject()->getEntity()->id();
   /* @var \Drupal\permissions_by_term\AccessStorage $access_storage */
   $access_storage = \Drupal::service('permissions_by_term.access_storage');
-  $access_storage->saveTermPermissions($formState, $termId);
-  /**
-   * @var \Drupal\permissions_by_term\NodeAccess $nodeAccess
-   */
-  $nodeAccess = \Drupal::service('permissions_by_term.node_access');
-  $nodeAccess->rebuildByTid($termId, $formState);
-  Cache::invalidateTags(['search_index:node_search']);
+  $access_update = $access_storage->saveTermPermissions($formState, $termId);
+
+  // Check if we need to rebuild node_access by term id
+  $update_node_access = false;
+
+  // Has anything has changed?
+  foreach($access_update as $values) {
+    if(!empty($values)) {
+      $update_node_access = true;
+      break;
+    }
+  }
+
+  // Do we rebuild node access?
+  if($update_node_access === true) {
+    /**
+     * @var \Drupal\permissions_by_term\NodeAccess $nodeAccess
+     */
+    $nodeAccess = \Drupal::service('permissions_by_term.node_access');
+    if ($nodeAccess->rebuildByTid($termId)) {
+      Cache::invalidateTags(['search_index:node_search']);
+    }
+  }
 }
 
 /**
Index: src/NodeAccess.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/NodeAccess.php	(revision c44a5e4143fe5b22f7eede58c7f6acb6bfd8e6fd)
+++ src/NodeAccess.php	(revision )
@@ -244,27 +244,23 @@
     $this->userInstance = $userInstance;
   }
 
-  public function rebuildByTid($tid, $formState) {
-    $nids = $this->accessStorage->getNidsByTid($tid);
-    if (!empty($nids)) {
-      $this->dropRecordsByNids($nids);
-    }
-
-    if (empty($this->accessStorage->getSubmittedUserIds()) && empty($this->accessStorage->getSubmittedRolesGrantedAccess($formState))) {
-      return;
-    }
-
-    foreach ($nids as $nid) {
-      $grants = $this->createGrants($nid);
-      foreach ($grants as $grant) {
-        $this->database->insert('node_access')
-          ->fields(
-            ['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete'],
-            [$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]
-          )
-          ->execute();
+  /**
+   * @param int $tid
+   * @return bool
+   *   True if access records have been rebuilt, false no.
+   */
+  public function rebuildByTid($tid) {
+    if (!empty($nids = $this->accessStorage->getNidsByTid($tid))) {
+      foreach ($nids as $nid) {
+        $this->dropRecordsByNids([$nid]);
+        $this->rebuildByNid($nid);
       }
+
+      return true;
+
     }
+
+    return false;
 
   }
 
