Copying a Graphic iOS

3968
4
Jump to solution
08-27-2014 01:37 PM
KyleDunaway
New Contributor III

Im having weird issues with copying graphics.

AGSGraphic *first = .....

AGSGraphic *second = [[AGSGraphic alloc] initWithJSON:[first encodeToJSON];

If I change some of the attributes of both first and second, update first, and add second to the feature layer, because second has the same globalID as first, I get two instances of the first graphic on the feature layer.

If I check the database, the features are all correct, and the second graphic did get added and the first got updated correctly. 

I have to reload the layers in the app to see the changes.  Any ideas what could be going wrong?

I checked the graphics, when i copy the first graphic and make a copy/second graphic they have the same globalID.

Thanks

0 Kudos
1 Solution

Accepted Solutions
DiveshGoyal
Esri Regular Contributor

When you use encodeToJSON, all the information of the graphic is serialized into json, including some attributes such as ObjectID and GlobalID which are supposed to be unique to each graphic. When you use this json to hydrate a new instance of the graphic, the same IDs are used which could cause problems. You should nil out these attributes of the new graphic before adding it to the layer.

View solution in original post

0 Kudos
4 Replies
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hello Kyle,

I am not really sure how do you want to arrange the two graphics display on the map. But ideally, each AGSGraphic should point to specific geometry, then based on the geometry you can associate the graphic to this unique Global ID.

For example if you check 10.2.3: AGSGraphic Class Reference  , it says "Only the geometry argument is mandatory." Therefore, I think you we using initWithJSON method, you suppose to do the same thing in order to make the graphic associate to specific Global ID.

For example this is snippet:

AGSGraphic *pinGraphic = [AGSGraphic graphicWithGeometry:pt symbol:pinSymbol attributes:nil infoTemplateDelegate:nil];

Also, have you checked with this thread yet? This thread might give you some ideas as well.

Update AGSFeatureLayer

Hope this can help.

0 Kudos
KyleDunaway
New Contributor III

Displaying the graphic isn't the problem I am having.

The problem is when I update the graphic, the copy is getting updated as well.

AGSGraphic *one = [[AGSGraphic alloc] init];

[one setAttribute:@"foo" forKey:@"Name"];

AGSGraphic *two = [[AGSGraphic alloc] initWithJSON:[one encodeToJSON];

[two setAttribute:@"bar" forKey@"Name"];

[featureLayer addGraphic:two];

[featureLayer addFeatures:@[two];

[featureLayer updatesFeatures:@[one]];

When I check featureLayer.graphics I have two copies for one, and no two graphic.

When I check the database, two is there.

If I close the app, and reload the webMap, into mapView, and get the layer again, the graphics are correct.

Im just confused on why I have two copies and one, and how I avoid this.

I tried [featureLayer refresh] to no avail.

Thanks

0 Kudos
DiveshGoyal
Esri Regular Contributor

When you use encodeToJSON, all the information of the graphic is serialized into json, including some attributes such as ObjectID and GlobalID which are supposed to be unique to each graphic. When you use this json to hydrate a new instance of the graphic, the same IDs are used which could cause problems. You should nil out these attributes of the new graphic before adding it to the layer.

0 Kudos
KyleDunaway
New Contributor III

Ahh, that would make sense.

It was an interesting error.

I would addGraphic, addFeature, updateFeature in that order and sometimes the add would work, and sometimes the update would overwrite it.

I ended up making a new graphic, and assigning the attributes one by one as I was getting frustrated.

If I decide to fix this, I should do this?

AGSGraphic *new = [[AGSGraphic alloc] initWithJSON:[old encodeToJSON];;

new setAttributeToNullForKey:[@"GlobalId"]; and OBJECTID

Correct?

Thanks for the reply

0 Kudos