简体   繁体   中英

How to check if an object has changed property in deserialisation c#

I am using JsonConvert to get data from my api every 15 seconds.

This data is going to an observable collection which is of type here .

I can't make use of the TrulyObservableCollection because every 15 second I am assigning the api to the collection as follows:

collection = JsonConvert.DeserializeObject<ManifestJSON>(uri)

Is there a way to check if an existing object in the DeserializeObject has change so I can modify my collection?

So I can use .Add and .Remove instead of overriding the collection.

It depends on your JSON format (obviously if your message includes the date then this won't work); but one possibility might be to compare the raw JSON before you deserialise it. It's certainly not foolproof, but if you're reading the stream from the same source, you might be okay with a string comparison.

You could iterate through the collections and compare the objects.

If you find a match, you update the corresponding object and if you don't you either add or remove it from the data-bound source collection.

You could perform the actual iterating on a background thread in order to keep the app responsive, but you will still need to add/remove items from the source collection back on the UI thread so doing this is probably pretty pointless from a performance perspective anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM