Update AGSFeatureLayer

3886
6
02-26-2012 10:43 PM
FaisalSabri
New Contributor
Hi ,

i'm trying to update AGSFeature , adding a feature is working with me but updating the same feature is not working

this the code i'm using For Update :


            NSLog(@"%@",CurrentFeatureGraphic);
          
           // Here i'm changing the Feature Attributes Only for the Description , i'm not changing any thing else

            NSMutableDictionary *graphicAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                      [NSString stringWithFormat:@"%@",MarkupDescription.text], @"Description",
                                                      [NSString stringWithFormat:@"%@",MarkupJobID.text], @"JOB_ID",
                                                      [NSString stringWithFormat:@"%@",Delegat.SelectedMarkupOobjecID], @"OBJECTID",
                                                     
                                                      nil];   
           
            AGSFeatureLayer *flayer = (AGSFeatureLayer*)FeatureLayer;
           
            CurrentFeatureGraphic.attributes = graphicAttributes;
        
            int oid = [flayer objectIdForFeature:CurrentFeatureGraphic];
           
            if(!flayer.editable){
                NSLog(@"Layer does not support editing");
                //bail out
            }
           
            if (oid > 0)
            {
                NSOperation *result = [flayer updateFeatures:[NSArray arrayWithObject:CurrentFeatureGraphic]];
               
                NSLog(@"result %@",result);
                [flayer dataChanged];
            }

this the code i'm using For Adding :


        AGSMutablePolygon *Polygon;
       
        for (AGSGraphic *g in graphicsLayer.graphics)
        {
            if ([g.geometry isKindOfClass:[AGSPolygon class]])
            {
                Polygon = [[[AGSMutablePolygon alloc] initWithSpatialReference:self.CurrentMap.spatialReference] autorelease];
                Polygon = g.geometry;
               
               
            }
           
        }

NSMutableDictionary *graphicAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                      [NSString stringWithFormat:@"%@",MarkupDescription.text], @"Description",
                                                      [NSString stringWithFormat:@"%@",MarkupJobID.text], @"JOB_ID",
                                                     
                                                      nil];   
           
            AGSFeatureLayer *flayer = (AGSFeatureLayer*)FeatureLayer;
           
            AGSGraphic *AddFeature = [[AGSGraphic alloc] initWithGeometry:Polygon symbol:nil attributes:graphicAttributes infoTemplateDelegate:self];
           
            NSOperation *result = [flayer addFeatures:[NSArray arrayWithObject:AddFeature]];
          
            [flayer dataChanged];




Thanks ,
0 Kudos
6 Replies
MelindaFrost
Occasional Contributor
Instead of trying to override the entire attribute dictionary object, try to update each of the fields like so:

CurrentFeatureGraphic.attributes = graphicAttributes;

[graphicAttributes setObject:MarkupDescription.text forKey:@"Description"];
0 Kudos
Nicholas-Furness
Esri Regular Contributor
Hi Melinda,

Might be a little cut-and-paste error in there. I think you mean something like:

NSMutableDictionary *graphicAttributes = CurrentFeatureGraphic.attributes;

[graphicAttributes setObject:MarkupDescription.text forKey:@"Description"];
...


Faisal: It's also worth noting that you probably shouldn't update the OBJECTID for an existing feature. This is auto-generated and shouldn't be updated (unless you've deliberately done something very odd to the feature class, and it's highly unlikely you'll encounter that).

And for added decoupling from the attributes dictionary or geodatabase schema, you should consider using AGSFeatureLayer's objectIdForFeature: method when reading the Object ID.

Nick.
0 Kudos
SteveLettau
New Contributor
I'm experiencing the same problem as Faisal.  I can confirm my service is working correctly since I can update an attribute through the REST interface via HTTP.  In my iOS, I'm using the following code:

    ...                
    [graphicToEdit.attributes setValue:@"John" forKey:@"INSP_NAMES"];
    [featureLayerToEdit updateFeatures:[NSArray arrayWithObject:graphicToEdit]];
    ...

When reviewing the result, SUCCESS is NO.

- (void)featureLayer:(AGSFeatureLayer *)featureLayer operation:(NSOperation *)op didFeatureEditsWithResults:(AGSFeatureLayerEditResults *)editResults {
    //Listen for results of edit operations.  If successful, redraw layer
    AGSEditResult *updateResult = [editResults.updateResults objectAtIndex:0];
    if (updateResult.success) {
        [featureLayer dataChanged];
    }
...


When debugging, the only error I can see is:

  _globalId = (NSNull *) 0x24a9cd8  Variable is not a CFString.


I have a GlobalID column in my table, and I've confirmed the ObjectID being passed is correct.  Forgive my ignorance, but I'm not sure if I need to be sending all attributes?  Or can I just update a single attribute like I'm trying.

Any help is appreciated!

Thanks,
Steve
0 Kudos
FaisalSabri
New Contributor
Dear Melinda ,

Thank you for the solution but it did not work ..

Dear Nick ,

I'm using "AGSFeatureLayer's objectIdForFeature:" in my upadte code to check if this Feature Exist or not , and for updating the OBJECTID , i'm not updating it at all , i know it's auto
generated , but if i modified only the @"Description" And @"JOB_ID" with out adding the OBJECTID to the attribute , it will give me a error with out going to the server ..

and by default when i use the "NSlog" to print the attributes for the graphic that i add it at the beging it's showing the OBJECTID between the attribute , but if i updated only the @"Description"@"JOB_ID" it will remove the OBJECTID from the Attribute , then how the Feature service will know which feature to update ??

as Steve said : I'm not sure if I need to be update all attributes? Or can I just update a single attribute ??

my application is doing the Following :

- the user will draw a polygon
- he will click on save edit to show up a form sheet to enter a description for the Polygon
- once he finished he will click on save button and save the feature to server

until here i'm not facing any problem ..

- after that if he have different features on the map , and he click on any one of them , i will show a form sheet with a description for that feature ( i will show the form only if he clicked on a graphic with polygon )
- now if he want to modified the description and then click save i'm trying to update the feature depending on the graphic attribute that the user clicked on the map .

this what i'm doning on my app

Any help is appreciated

Thanks,

Faisal
0 Kudos
NimeshJarecha
Esri Regular Contributor
Faisal,

The attributes property of the feature should include the object id (and the global id, if available) of the feature along with the other attributes (desired). Also, you should include the geometry.

Regards,
Nimesh
0 Kudos
SteveLettau
New Contributor
Faisal, I got it (with a little help from ESRI tech support).  Turns out I was passing with wrong OBJECT_ID for the graphic I thought was the right one.  Here's what I had:

    ...                
    [graphicToEdit.attributes setValue:@"John" forKey:@"INSP_NAMES"];
    [featureLayerToEdit updateFeatures:[NSArray arrayWithObject:graphicToEdit]];
    ...


After further investigation, it turned out I was passing the wrong OBJECT_ID in the graphicToEdit.attributes array.  I needed to get the correct OBJECT_ID using lookupFeatureWithObjectId.  I changed the code to this and attribute is updated:

    ...
    int oid = [featureLayerToEdit objectIdForFeature:graphicToEdit];
    AGSGraphic *editFeature = [featureLayerToEdit lookupFeatureWithObjectId:oid];
    [editFeature.attributes setValue:@"John" forKey:@"INSP_NAMES"];
    [featureLayerToEdit updateFeatures:[NSArray arrayWithObject:editFeature]];
    ...


Thanks to Garima V. in tech support.

Steve
0 Kudos