【问题标题】:Microsoft Azure Mobile Service - Xamarin.Android Offline Sync IssuesMicrosoft Azure 移动服务 - Xamarin.Android 脱机同步问题
【发布时间】:2016-05-29 12:53:06
【问题描述】:

我正在尝试为 Android 应用设置 Azure 移动服务。我从 Azure 中提供的快速入门示例开始。我创建了一个数据库和服务器连接,托管了一个 Node.js 后端,并使用我尝试成功访问的表设置了数据库。我已按照 Microsoft 提供的有关该主题的各种教程进行操作,但仍然无法连接到我的数据库。运行我的应用程序后,会出现一个对话框,显示消息“推送操作失败。有关详细信息,请参阅 PushResult”。这是我认为访问数据库所需的代码 sn-ps。

初始化:

        //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.

    }

来自 SyncAsync:

            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()。根据我看到的其他样本,这看起来是正确的。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • “查看 PushResult 了解详情” - 它告诉你什么?
  • 在哪里可以找到 PushResult。尝试同步后是否需要手动记录?目前我只是捕捉任何异常并输出消息。在研究了 PushResult 之后,我读到一个线程,本地存储中的任何错误都会导致同步失败。我将添加一个我的表类型定义的 sn-p。
  • 我假设 PushResult 是 PushAsync() 的返回值。我没有使用过 Azure 服务,所以我真的不确定。

标签: c# android azure xamarin synchronization


【解决方案1】:

请看下面的代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多