Intermittent problem with EditOperation

760
5
07-25-2018 05:55 AM
BrianBulla
Occasional Contributor III

Hi,

I have a button with some code behind it that edits the attributes of selected features when run.  I am having an intermittent issue where sometimes the code runs and works properly, and other times it runs and only seems to partially complete.  I don't get any error messages in either case, and in each case my code gets all the way to the end and displays a MessageBox with the results.  But, when it doesn't work I can see that the attributes on the selected features have not been updated and the process is also not available to 'roll-back' via the undo button (ie. not on the undo-stack).

I have posted the code below, but was just wondering if anyone else has seen this issue.  When I notice that the code hasn't worked, I just hit the button again (without changing anything related to my selection in the map) and then it all works fine.  I just don't know why it won't work that first time.

Also, this is intermittent.  Sometimes it works the first time, sometimes it doesn't.  I cannot find any pattern.

       protected async override void OnClick()
        {
            try
            {
                int updatedFeatures = 0;

                await QueuedTask.Run(() =>
                {
                    //Start the edit operation, and place on the UNDO stack
                    var editOperation = new ArcGIS.Desktop.Editing.EditOperation();
                    editOperation.Name = "Update Fittings Diameters on Selected Features";
                    editOperation.EditOperationType = ArcGIS.Desktop.Editing.EditOperationType.Long;

                    Map map = MapView.Active.Map;

                    FeatureLayer waterMainLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WaterMain").FirstOrDefault();
                    FeatureLayer waterServiceLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WaterService").FirstOrDefault();
                    FeatureLayer hydrantLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_Hydrant").FirstOrDefault();
                    FeatureLayer controlValveLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_ControlValve").FirstOrDefault();
                    FeatureLayer waterFittingsLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_Fitting").FirstOrDefault();
                    FeatureLayer reliefValveLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_ReliefValve").FirstOrDefault();
                    FeatureLayer pressureZoneLayer = map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(l => l.Name == "GISWRKS1.WORKS.WAT_PressureZone").FirstOrDefault();


                    #region Check for Relief Valves
                    //Check for the Relief valves layer to be in the map
                    if (reliefValveLayer != null)
                    {
                        //Check to see if there is a selection
                        if (reliefValveLayer.SelectionCount > 0)
                        {
                            //Get the OIDs of any selected Relief Valves
                            var reliefValvesSelection = reliefValveLayer.GetSelection();
                            IReadOnlyList<long> selectedOIDs = reliefValvesSelection.GetObjectIDs();

                            //Create an Inspector to edit the selected features
                            var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector();

                            //Loop through each selected OID
                            foreach (var oid in selectedOIDs)
                            {
                                //Load the current Relief Valve into the inspector
                                insp.Load(reliefValveLayer, oid);

                                //Get the coordinate of the current Relief Valve
                                MapPoint reliefValvePoint = (MapPoint)insp.Shape;

                                //Could be intersecting a WaterService or a WaterMain.
                                //Do for WaterServices first:
                                SpatialQueryFilter wsFilter = new SpatialQueryFilter();
                                wsFilter.FilterGeometry = reliefValvePoint;
                                wsFilter.SpatialRelationship = SpatialRelationship.Intersects;
                                wsFilter.SubFields = "*";

                                //Loop through each intersecting WaterService using a RowCursor.
                                RowCursor wsCursor = waterServiceLayer.Search(wsFilter);
                                Feature wsFeature;

                                //Go to the first item in the cursor, and use it as a Feature
                                wsCursor.MoveNext();
                                wsFeature = (Feature)wsCursor.Current;

                                if (wsFeature != null)
                                {
                                    insp["Diameter"] = wsFeature["Diameter"];
                                    insp["WATERMAINID"] = wsFeature["FACILITYID"];

                                    //Update these attributes on the Relief Valve from the WaterService, if required
                                    if (Properties.Settings.Default.fitContractNumberSetting == true)
                                        insp["CONTRACTNO"] = wsFeature["CONTRACTNO"];

                                    if (Properties.Settings.Default.fitInstallDateSetting == true)
                                        insp["INSTALLDATE"] = wsFeature["INSTALLDATE"];

                                    if (Properties.Settings.Default.fitDataSourceSetting == true)
                                        insp["DATASOURCE"] = wsFeature["DATASOURCE"];

                                    if (Properties.Settings.Default.fitLifecycleSetting == true)
                                        insp["LifecycleStatus"] = wsFeature["LifecycleStatus"];                                    
                                }   
                                //end of if wsFeature loop

//Add code for WM here ****

                                editOperation.Modify(insp);
                                updatedFeatures++;
                            }
                            //end of foreach Relief Valve loop
                        }
                        //end of if selected Relief Valve > 0 loop
                    }
                    //end of if Relief Valve layer != null loop

                    #endregion

                    editOperation.Execute();
                });

                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(updatedFeatures.ToString() + " features updated.", "Fittings Tool - Complete");
            }

            catch
            {

            }

            finally
            {

            }
        }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
5 Replies
BrianBulla
Occasional Contributor III

Also, here are some screen shots of the problem.  In the first one the MessageBox tells me that the code ran because the updatedFeatures counter has increased to 2 (2 selected features), but the edits didn't take because the Diameter is still at 300mm (default) and the WATERMAINID has not changed from default.

In the second screenshot everything has worked fine.  The MessageBox says the same thing (2 updated features), but you can actually see where the Diameter and WATERMAINID have been updated correctly.  Also, the 'edit' shows up in the Undo button (not shown).

In both cases the EditOperation.Execute must have been executed, since the MessageBox is the next line of code.  I'm just not sure why it isn't actually doing anything.  What I also notice is that when it does work the Attributes pane kind of flickers, but when it doesn't work nothing happens in the Attribute pane.  In those cases I also try hitting the 'Refresh' button on the map pane, but it makes no difference.

0 Kudos
NarelleChedzey
Esri Contributor

Brian, 

Try adding a 'using' around your Search line to ensure the rowCursor is disposed of correctly

eg. 

using (RowCursor wsCursor = waterServiceLayer.Search(wsFilter))
{
   Feature wsFeature;

   //Go to the first item in the cursor, and use it as a Feature
   wsCursor.MoveNext();
   wsFeature = (Feature)wsCursor.Current;

   if (wsFeature != null)
   {
       ...
   }
}

The other thing to try would be to check the return value of editOperation.Execute  and editOperation.ErrorMessage if you get a failure. 

Narelle

BrianBulla
Occasional Contributor III

Hi Narelle Chedzey‌.  Thanks for the tip on using the 'Using' statement.  I have now included that in my code, as well as modifying the editOperation.Execute as follows:

                    if (editOperation.Execute() == true)
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(updatedFeatures.ToString() + " features updated.", "Fittings Tool - Update Complete");
                    else
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(editOperation.ErrorMessage.ToString(), "Fittings Tool - ERROR");

Unfortunately, I am still occasionally getting the error come up.  Are there any other ways to narrow down what might be happening??  After getting the error I just click on the button to fire the code again, and it works successfully.  Generally this happens when I am running the tool in Debug mode the first time I try to run it.  

0 Kudos
NarelleChedzey
Esri Contributor

Hi Brian, 

Are you working with SDE or a file geodatabase?  Standard featureclasses or feature services?   Any additional information about the data you can share - relationships, networks?   

If it's a file geodatabase with standard feature classes is there any chance you could post the data (or a subset where you can still duplicate the sporadic problem), it might just help us duplicate the problem a little easier,   If the data is proprietary I understand if you can't post it, 

Thanks

Narelle

0 Kudos
BrianBulla
Occasional Contributor III

Hi Narelle Chedzey,

The data is in SDE.  Standard features classes...nothing too fancy going on here.  No relationships or networks.  There are some topology rules on the data, but my tool only edits attributes, not geometry.

I'm still experiencing the intermittent editing issues when running the tool.  But then if I try again a few seconds later it works fine.

Generally we don't give out data without a data licence agreement, but if there are some specific things you want me to try, just let me know.

0 Kudos