Bug 34118 - Updating a record in an OData service: method “BeginSaveChanges” does not fire the callback
Summary: Updating a record in an OData service: method “BeginSaveChanges” does not fir...
Status: RESOLVED INVALID
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-09-21 10:54 UTC by Filip Drsek
Modified: 2016-01-26 11:32 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Filip Drsek 2015-09-21 10:54:48 UTC
I have access to an OData v3 service. I know about a particular data table named “Persons” in the service and I have the URI ending with “Persons” to access that table. I know that this table contains a few instances of a particular class named “Customer”. I know the properties defined in this class, for example “FirstName”, “LastName”, “SysModifiedOn”. I am trying to load data from this data table and to modify one of these instances and save this modification into the OData service.

In my Visual Studio project in Service References I have a reference to the OData service, named “MyService”. I can access the “MyService” class, which inherits from the “System.Data.Services.Client.DataServiceContext” class (package “Microsoft.Data.Services.Client” installed with NuGet). My colleague told me that I should use the “BeginExecute” method referencing a callback method calling the “EndExecute” method to retrieve items from the service, and the “BeginSaveChanges” and “EndSaveChanges” methods to save the modifications. I tried this code to load data from the “Persons” table in the service, to modify one item and to update it in the service. I call the “CreateConnection” method.

        private MyService.Model client;
        private ObservableCollection<Customer> customers;

        private void CreateConnection()
        {
            this.client = new Model(new Uri("http://.../.../odata.v3/default"));
            this.LoadCustomers();
        }

        private void LoadCustomers()
        {
            if (this.customers == null)
            {
                this.customers = new ObservableCollection<Customer>();
            }

            this.customers.Clear();

            try
            {
                var query = this.client.CreateQuery<Customer>("Persons");
                var results = query.BeginExecute(this.OnCustomersQueryComplete, query);
            }
            catch (Exception ex)
            {

            }
        }

        private void OnCustomersQueryComplete(IAsyncResult result)
        {
            var query = (System.Data.Services.Client.DataServiceQuery<Customer>)result.AsyncState;
            Customer toModify = null;

            try
            {
                foreach (Customer c in query.EndExecute(result))
                {
                    this.customers.Add(c);
                    this.Status += Environment.NewLine + string.Format("loaded object: [{0}.{1}] {2} {3}", c.SysModifiedOn.ToString(), c.SysModifiedOn.Millisecond.ToString("000"), c.FirstName, c.LastName);
                    if (c.FirstName.StartsWith("John"))
                    {
                        toModify = c;
                    }
                }
            }
            catch (Exception ex)
            {

            }

            if (toModify != null)
            {
                toModify.FirstName = "Johnny";
                toModify.SysModifiedOn = DateTime.Now;
                this.SaveChanges(toModify);
            }
            else
            {
                this.Status += Environment.NewLine + "Nothing to modify.";
            }
        }

        private void SaveChanges(object o)
        {
            this.Status += Environment.NewLine + "Updating…";
            this.client.UpdateObject(o);
            this.client.SaveChangesDefaultOptions = System.Data.Services.Client.SaveChangesOptions.PatchOnUpdate;
            IAsyncResult result = this.client.BeginSaveChanges(this.OnClientSaveChanges, this.client);
        }

        private void OnClientSaveChanges(IAsyncResult result)
        {
            this.Status += Environment.NewLine + "Update started!";
            this.client.EndSaveChanges(result);
            this.Status += Environment.NewLine + "Updated!";
        }

This code is a part of my ViewModel, which has a string property “Status”, which is displayed in the user interface through binding.

If I put this code into a Xamarin.Forms application and I call the “CreateConnection” method, I see the list of items loaded from the service, then the “Updating…” message and nothing more. Apparently, the “OnClientSaveChanges” method is not invoked.

In a Windows application (for example Console application), however, the code completes and the modification is saved in the OData service. I need the same to work in a Xamarin.Forms application.
Comment 1 Rui Marinho 2016-01-26 11:32:37 UTC
Thank you for taking the time to submit the bug. We tried to reproduce the issue you reported but were unable given the description at first look this doesn't seem to be related with Xamarin.Forms, please provide a sample that shows the problem with xamarin forms and reopen if necessary. If so please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue we would very much appreciate it.

Warm regards,
Xamarin Forms Team