我正在尝试为Android应用设置Azure移动服务。我开始使用Azure中提供的快速入门示例。我创建了一个数据库和服务器连接,托管了一个Node.js后端,并使用我试图成功访问的表设置数据库。我已经按照Microsoft对此主题的各种教程进行了操作,但仍然无法连接到我的数据库。运行我的应用程序后,会出现一个对话框,显示消息“推送操作失败。有关详细信息,请参阅PushResult”。以下是我认为访问数据库所需的代码片段。
初始化:
someMethod()
来自SyncAsync:
//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());
总结一下。我用我的URL创建了我的移动服务客户端。初始化本地商店。将同步表分配给数据库中的表。然后调用SyncAsync()。根据我见过的其他样本,这看起来是正确的。任何帮助,将不胜感激。感谢。
答案 0 :(得分:0)
请查看以下代码:
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"]);
}
}
}
您可以在PushResult
列表中找到syncErrors
。