Adding and Deleting Items from collection

Mar 14, 2012 at 1:26 PM

I am developing an application that allows the user to view/add/edit/delete data in multiple collections. The items in the collections are first retrieved from a web service with just the core information (things like title, id, description.) I register a proxy for each item as they are added to a collection in order to implement a master detail scenario.

All works fine when I view/edit an item's detail and update a property - the views update as they should with any edited values.

The issue I am having is when a user adds or deletes an item on the phone - the new item is not displayed and the deleted item remains visible. I have had to resort to reloading the data from the service to keep the lists in sync - obviously not good

Here is what I tried-

Adding an item

  • send data to service and wait for success return code
  • DataManager.Current.Save<...>(...) - I implemented IDataOptimizer on my viewmodel, DeserializeOptimizedData never gets called, SerializeOptimizedData does)
  • DataManager.Current.RegisterProxy<...>(...) - figured I should do this so that the lists would update

Deleting an Item

  • delete the item calling the service and wait for success return code
  • DataManager.Current.Clear<...>(...)
  • DataManager.Current.Flush()

I am sure I am missing something
Michael

Coordinator
Mar 15, 2012 at 8:56 PM

Hi Michael -

I think what you're missing here is the behavior of an item versus the collection.  If I have a collection of items and I databind that to an ItemsSource, the containing object (say, an ItemsControl) has a reference to the ObservableCollection that was pushed in via the databind.  It doesn't have any visibility into the items in the collection - it's just listening to CollectionChanged events.

So you can mess with the item, and it will update values that are bound to item properties, as you're doing.  But if you want to affect the container, you need to go to the collection itself and remove/add the value.

So in your case, I assume you'd do something like

var newItem = new ProductViewModel();

//...

DataManager.Current.Save(newItem)

var myCollection = DataManager.Current.Load<Products>("23424");

myCollection.Add(newItem);

// now you need to decide whether to do a Save on the Collection or just an Invalidate so that next time someone asks for it, they get a fresh copy from the server.


I'm also not sure you need the proxies in this case since you created the full value.

Thanks,


Shawn