user3348354
user3348354

Reputation: 321

In Xamarin azure, UpdateAsync is not updating database, only changes info locally

So my app updates the information inside locally when i call UpdateSync on the table but it doesn't update the online database? Am I doing something wrong?

IMobileServiceSyncTable<Models.About_user> about_user_table;
Update_my_table(Object Item)
{                
   Models.About_user About_user = (Models.About_user)Item;
   await about_user_table.UpdateAsync(About_user);
   IMobileServiceSyncTable<Models.About_user> about_user_table;
}

Upvotes: 0

Views: 540

Answers (2)

user3348354
user3348354

Reputation: 321

So All I had to is put that on the bottom of my updatesync. Note: this didnt work unless I explicitly had a variable in my about_user model called version like [JsonProperty(PropertyName = "Version")] public string Version { get; set; }

  await about_user_table.UpdateAsync(About_user);
            try
            {
                await Client.SyncContext.PushAsync();
              ///  await about_user_table.PullAsync("all About_user", about_user_table.CreateQuery());
            }
           catch (MobileServicePushFailedException ex)
           {
                if (ex.PushResult != null)
               {
                   foreach (var error in ex.PushResult.Errors)
                   {
                      await ResolveConflictAsync(error,"about_user_table");
                   }
                }
            }
    async Task ResolveConflictAsync(MobileServiceTableOperationError error, string table_name)
    {
        var serverItem = error.Result.ToObject<About_user>();
        var localItem = error.Item.ToObject<About_user>();



        // Note that you need to implement the public override Equals(TodoItem item)
        // method in the Model for this to work
        if (serverItem.Equals(localItem))
        {
            // Items are the same, so ignore the conflict
            await error.CancelAndDiscardItemAsync();
            return;
        }

        // Client Always Wins
        localItem.Version = serverItem.Version;
        await error.UpdateOperationAsync(JObject.FromObject(localItem));
    }

Upvotes: 0

Bruce Chen
Bruce Chen

Reputation: 18465

According to your code, you are using the sync table (IMobileServiceSyncTable), for the UpdateAsync operation, it would update your local SQLite database. In order to update your online database, you need to execute the Push operation by using the following code:

await Client.SyncContext.PushAsync();

Note: when executing the push operation, you may need to handle Conflict Resolution. For more details, you could refer to adrian's book about Handling Conflict Resolution and An Offline Client.

Moreover, you could use client.GetTable<Model>() for constructing an online table and do CUD changes against your online table. For more details, you could refer to here. Additionally, you could follow here about offline synchronization.

Upvotes: 0

Related Questions