Microsoft Azure Mobile Service - Xamarin.Android 脱机同步问题
本文关键字:Android 脱机 同步 问题 Xamarin Azure Mobile Service Microsoft | 更新日期: 2023-09-27 18:35:07
我正在尝试为Android应用程序设置Azure移动服务。我从 Azure 中提供的快速入门示例开始。我已经创建了一个数据库和服务器连接,托管了一个 Node.js 后端,并使用我尝试成功访问的表设置了数据库。我已经遵循了Microsoft关于该主题的各种教程,但我仍然无法连接到我的数据库。运行我的应用后,将显示一个对话框,其中包含消息"推送操作失败。有关详细信息,请参阅推送结果"。以下是我认为访问数据库所需的代码片段。
初始化:
//Mobile Service Client reference
private MobileServiceClient client;
//Mobile Service sync table used to access data
private IMobileServiceSyncTable<EmployeeItem> employeeSyncTable;
private IMobileServiceSyncTable<EventItem> eventSyncTable;
private IMobileServiceSyncTable<RecipientListItem> recipientListSyncTable;
//Adapter to map the items list to the view
private EmployeeItemAdapter employeeItemAdapter;
private EventItemAdapter eventItemAdapter;
private RecipientListItemAdapter recipientListItemAdapter;
const string applicationURL = @"myurl(this is correct)";
const string localDbFilename = "localstore.db";
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Login);
CurrentPlatform.Init();
// Create the Mobile Service Client instance, using the provided
// Mobile Service URL
client = new MobileServiceClient(applicationURL, new NativeMessageHandler());
await InitLocalStoreAsync();
// Get the Mobile Service sync table instance to use
employeeSyncTable = client.GetSyncTable<EmployeeItem>();
eventSyncTable = client.GetSyncTable<EventItem>();
recipientListSyncTable = client.GetSyncTable<RecipientListItem>();
// Create an adapter to bind the items with the view
employeeItemAdapter = new EmployeeItemAdapter(this, Resource.Layout.Employee);
eventItemAdapter = new EventItemAdapter(this, Resource.Layout.Event);
recipientListItemAdapter = new RecipientListItemAdapter(this, Resource.Layout.RecipientList);
//using this to test my connection
await employeeSyncTable.InsertAsync(makeSampleEmployeeItem());
// Load the items from the Mobile Service
OnRefreshItemsSelected();//calls SyncAsync() and RefreshItemsFromTableAsync() which updates views.
}
从同步异步:
await client.SyncContext.PushAsync();
await employeeSyncTable.PullAsync("allEmployeeItems", employeeSyncTable.CreateQuery());
await eventSyncTable.PullAsync("allEventItems", eventSyncTable.CreateQuery());
await recipientListSyncTable.PullAsync("allRecipientListItems", recipientListSyncTable.CreateQuery());
表类型定义:
public class EmployeeItem
{
public string Id { get; set; }
[JsonProperty(PropertyName = "Name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "Email")]
public string Email { get; set; }
[JsonProperty(PropertyName = "EmployeeID")]
public int EmployeeID { get; set; }
[JsonProperty(PropertyName = "Department")]
public string Department { get; set; }
[JsonProperty(PropertyName = "PrivledgeLevel")]
public string PrivledgeLevel { get; set; }
}
所以总结一下。我使用我的 URL 创建移动服务客户端。初始化本地存储。将同步表分配给数据库中的表。然后调用 SyncAsync()。根据我看到的其他样本,这看起来是正确的。任何帮助将不胜感激。谢谢。
请看下面的代码:
public async Task SyncAsync()
{
ReadOnlyCollection<MobileServiceTableOperationError> syncErrors = null;
try
{
await this.client.SyncContext.PushAsync();
await this.todoTable.PullAsync(
//The first parameter is a query name that is used internally by the client SDK to implement incremental sync.
//Use a different query name for each unique query in your program
"allTodoItems",
this.todoTable.CreateQuery());
}
catch (MobileServicePushFailedException exc)
{
if (exc.PushResult != null)
{
syncErrors = exc.PushResult.Errors;
}
}
// Simple error/conflict handling. A real application would handle the various errors like network conditions,
// server conflicts and others via the IMobileServiceSyncHandler.
if (syncErrors != null)
{
foreach (var error in syncErrors)
{
if (error.OperationKind == MobileServiceTableOperationKind.Update && error.Result != null)
{
//Update failed, reverting to server's copy.
await error.CancelAndUpdateItemAsync(error.Result);
}
else
{
// Discard local change.
await error.CancelAndDiscardItemAsync();
}
Debug.WriteLine(@"Error executing sync operation. Item: {0} ({1}). Operation discarded.", error.TableName, error.Item["id"]);
}
}
}
您可以在syncErrors
列表中找到PushResult
。