AnsweredAssumed Answered

Intermittent problem with EditOperation

Question asked by bulla_b on Jul 25, 2018
Latest reply on Aug 7, 2018 by bulla_b



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()
                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
                                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 ****

                            //end of foreach Relief Valve loop
                        //end of if selected Relief Valve > 0 loop
                    //end of if Relief Valve layer != null loop



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