BatchUpdateAsync on CustomAdaptor only hit once

I was trying to edit a row. After clicking the save button on the modal window that hits BatchUpdateAsync in the CustomAdaptor and saves it to the database. But that only happens for the first time. After that, it never happens when I edit a row until I restart the whole application. I am providing the snippets here:


the razor snippet:

  1. @using Syncfusion.Blazor.Gantt
  2. @using Syncfusion.Blazor.Data
  3. @using Syncfusion.Blazor
  4.  
  5.  
  6. <SfGantt @ref="Gantt"
  7.          DataSource="@Models"
  8.          Height="450px"
  9.          Width="100%"
  10.          Toolbar="@(new List<string>() { "Add", "Edit", "Update", "Delete", "Cancel" })"
  11.          AllowReordering="true"
  12.          ShowColumnMenu="true"
  13.          AllowSelection="true"
  14.          EnableContextMenu="true"
  15.          AllowFiltering="true"
  16.          AllowSorting="true"
  17.          AllowResizing="true"
  18.          TaskMode="ScheduleMode.Manual"
  19.          EnablePredecessorValidation="true">
  20.     <SfDataManager Adaptor="Adaptors.CustomAdaptor" DataType="TaskData">
  21.         <CustomDataAdaptor></CustomDataAdaptor>
  22.     </SfDataManager>
  23.     <GanttTaskFields Id="Id" Name="Name" StartDate="StartDate" EndDate="EndDate"
  24.     ParentID="ParentId"
  25.     Duration="Duration" Progress="Progress" Child="Activities">
  26.     <GanttEditSettings Mode="EditMode.Dialog" AllowAdding="true" AllowDeleting="true" AllowEditing="true" AllowTaskbarEditing="true" ShowDeleteConfirmDialog="true">
  27.     </GanttEditSettings>
  28.     </GanttTaskFields>
  29. </SfGantt>

the adaptor class :

  1. public class CustomDataAdaptor: DataAdaptor
  2.     {
  3.  
  4. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  5.         [Inject] private ILogger<CustomDataAdaptor> Logger { get; set; }
  6.  
  7.         [Inject] private ProjectState ProjectState { get; set; }
  8.  
  9.         [Inject] private IContentService<Phase> PhaseService { get; set; }
  10.  
  11.         [Inject] private IToastService ToastService { get; set; }
  12.  
  13.         [Inject] private IContentService<Activity> ActivityService { get; set; }
  14. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  15.  
  16.         private CancellationTokenSource TokenSource { get; set; } = new();
  17.  
  18.         public override async Task<object> UpdateAsync(DataManager dataManager, object data, string keyField, string key)
  19.         {
  20.             var content = data as TaskData;
  21.            
  22.             if(content is not null)
  23.             {
  24.                 if (content.ParentId is null)
  25.                 {
  26.                     try
  27.                     {
  28.                         var phase = ProjectState?.Phases?.Find(p => p.Id == content.DataId);
  29.                         phase = phase ?? throw new Exception("No related phase found");
  30.  
  31.                         phase.Name = content.Name;
  32.                         phase.PhaseDuration = new PhaseDuration
  33.                         {
  34.                             Start = content.StartDate,
  35.                             End = content.EndDate
  36.                         };
  37.                         phase.Progress = content.Progress;
  38.  
  39.                         phase = await PhaseService.UpdateAsync(phase, TokenSource.Token);
  40.                         ProjectState?.LoadPhases(new[] { phase }, this);
  41.                     }
  42.                     catch (Exception ex)
  43.                     {
  44.                         Logger.LogError("{file} exception while updating phase {ex}", this, ex);
  45.                         ToastService.ShowError("Error while updating phase", "ERROR!!!!");
  46.                     }
  47.                 }
  48.                 else
  49.                 {
  50.                     try
  51.                     {
  52.                         var activity = ProjectState?.Activities?.Find(p => p.Id == content.DataId);
  53.                         activity = activity ?? throw new Exception("No related activity found");
  54.  
  55.                         activity.Name = content.Name;
  56.                         activity.StartDate = content.StartDate;
  57.                         activity.EndDate = content.EndDate;
  58.                         activity.Progress = content.Progress;
  59.  
  60.                         activity = await ActivityService.UpdateAsync(activity, TokenSource.Token);
  61.                         ProjectState?.LoadActivities(new[] { activity }, this);
  62.                     }
  63.                     catch (Exception ex)
  64.                     {
  65.                         Logger.LogError("{file} exception while updating activity {ex}", this, ex);
  66.                         ToastService.ShowError("Error while updating activity", "ERROR!!!!");
  67.                     }
  68.                 }
  69.             }
  70.  
  71.             return base.UpdateAsync(dataManager, data, keyField, key);
  72.         }
  73.  
  74.         public override async Task<object> BatchUpdateAsync(DataManager dataManager, object changedRecords, object addedRecords, object deletedRecords, string keyField, string key, int? dropIndex)
  75.         {
  76.             List<TaskData> addRecord = addedRecords as List<TaskData> ?? new();
  77.             List<TaskData> changed = changedRecords as List<TaskData> ?? new();
  78.             List<TaskData> deleteRecord = deletedRecords as List<TaskData> ?? new();
  79.  
  80.             if(changed is not null && changed.Count > 0)
  81.             {
  82.                 Logger.LogDebug("{file} edited records before edit : {changed}", this, JsonSerializer.Serialize(changed));
  83.  
  84.                 List<Task<Phase>> phaseTasks = new();
  85.                 List<Task<Activity>> activityTasks = new();
  86.  
  87.                 foreach(var content in changed)
  88.                 {
  89.                     if (content is null)
  90.                         continue;
  91.                     if (content.ParentId is null)
  92.                     {
  93.                         try
  94.                         {
  95.                             var phase = ProjectState?.Phases?.Find(p => p.Id == content.DataId);
  96.                             phase = phase ?? throw new Exception("No related phase found");
  97.  
  98.                             phase.Name = content.Name;
  99.                             phase.PhaseDuration = new PhaseDuration
  100.                             {
  101.                                 Start = content.StartDate,
  102.                                 End = content.EndDate
  103.                             };
  104.                             phase.Progress = content.Progress;
  105.  
  106.                             var phaseTask = PhaseService.UpdateAsync(phase, TokenSource.Token);
  107.                             phaseTasks.Add(phaseTask);
  108.                         }
  109.                         catch (Exception ex)
  110.                         {
  111.                             Logger.LogError("{file} exception while updating phase {ex}", this, ex);
  112.                             ToastService.ShowError("Error while updating phase", "ERROR!!!!");
  113.                         }
  114.                     }
  115.                     else
  116.                     {
  117.                         try
  118.                         {
  119.                             var activity = ProjectState?.Activities?.Find(p => p.Id == content.DataId);
  120.                             activity = activity ?? throw new Exception("No related activity found");
  121.  
  122.                             activity.Name = content.Name;
  123.                             activity.StartDate = content.StartDate;
  124.                             activity.EndDate = content.EndDate;
  125.                             activity.Progress = content.Progress;
  126.  
  127.                             var activityTask = ActivityService.UpdateAsync(activity, TokenSource.Token);
  128.                             activityTasks.Add(activityTask);
  129.                         }
  130.                         catch (Exception ex)
  131.                         {
  132.                             Logger.LogError("{file} exception while updating activity {ex}", this, ex);
  133.                             ToastService.ShowError("Error while updating activity", "ERROR!!!!");
  134.                         }
  135.                     }
  136.                 }
  137.  
  138.                 var phases = await Task.WhenAll(phaseTasks);
  139.                 ProjectState.LoadPhases(phases, this);
  140.                 var activities = await Task.WhenAll(activityTasks);
  141.                 ProjectState.LoadActivities(activities, this);
  142.  
  143.                
  144.                 Logger.LogDebug("{file} edited phases after edit : {changed}",this, JsonSerializer.Serialize(phases));
  145.                 Logger.LogDebug("{file} edited activities after edit : {changed}",this, JsonSerializer.Serialize(activities));
  146.             }
  147.  
  148.            
  149.  
  150.             return base.BatchUpdateAsync(dataManager, changedRecords, addedRecords, deletedRecords, keyField, key, dropIndex);
  151.         }
  152.     }

1 Reply

MS Monisha Sivanthilingam Syncfusion Team November 9, 2021 12:34 PM UTC

Hi Syed, 
 
Greetings from Syncfusion support. 
 
We have analyzed your issue. However, we were unable to replicate the issue you reported. The BatchUpdate method was triggered every time we performed Add/Edit/Delete actions and not just the first time as you said. Please share with us more details of your issue such as: 
 
  1. The steps taken to replicate the issue.
  2. A video replicating the issue.
  3. A screenshot of the error with complete stack trace(if any).
  4. If possible, please modify the below sample to replicate your issue or share an issue reproducible sample of your own.
 
 
Regards, 
Monisha. 


Loader.
Up arrow icon