【问题标题】:Fetch Azure sync table on new device在新设备上获取 Azure 同步表
【发布时间】:2017-02-22 01:34:03
【问题描述】:

我为 SyncTables 设置了 Azure 应用服务。在我的开发机器上,我可以保存项目并将它们同步到云端,没问题。但是,我很难将数据传输到新设备。我假设当第一次在新设备上加载应用程序时,table.PullAsync() 调用会获取每个表的所有数据。

推/拉过程成功完成,但没有数据被拉到设备。我可以在新设备上添加一个新项目,并将其上传到云端,但我没有任何运气将初始数据放到新设备上。这是我的SyncAsync 方法:

private IMobileServiceSyncTable<Person> personTable = App.MobileService.GetSyncTable<Person>();
private IMobileServiceSyncTable<Card> cardTable = App.MobileService.GetSyncTable<Card>();

private async Task SyncAsync(bool showMessage)
{
    string errorString = null;

    try
    {
        await App.MobileService.SyncContext.PushAsync();
        await personTable.PullAsync("person", personTable.CreateQuery());
        await cardTable.PullAsync("cards", cardTable.CreateQuery());
    }
    catch (MobileServicePushFailedException ex)
    {
        errorString = "Push failed because of sync errors: " +
            ex.PushResult.Errors.Count + " errors, message: " + ex.Message;

        errorString = ex.PushResult.Errors.Aggregate(errorString, (current, error) => current + ("\r\n" + error.Result));
    }
    catch (Exception ex)
    {
        errorString = "Pull failed: " + ex.Message +
            "\n\nIf you are still in an offline scenario, " +
            "you can try your Pull again when connected with your Mobile Service.";
    }

    if (errorString != null)
    {
        // TODO: log some sort of error message
        MessageBox.Show(errorString);
    } 
    else
    {
        if (showMessage)
        {
            MessageBox.Show("Database synchronization complete.");
        }
        LastSynced = string.Format("Last Synced: {0}", DateTime.Now.ToString());
    }
}

正如我所说,创建的新数据将同步到新设备,但是当应用程序安装在新设备上时,我需要能够对数据进行初始提取。有没有办法强制PullAsync() 调用获取所有数据,或者以某种方式让SyncTables 知道他们有数据要获取,以便下次调用SyncAsync 时,他们会获取数据?

这是我的InitLocalStoreAsync() 方法,它在应用程序启动时初始化本地数据库。我还尝试删除 localstore.db 文件并再次启动,希望文件的新副本想要提取所有数据,但事实并非如此。

private async Task InitLocalStoreAsync()
{
    if (!MobileService.SyncContext.IsInitialized)
    {
        var store = new MobileServiceSQLiteStore("localstore.db");
        store.DefineTable<Person>();
        store.DefineTable<Card>();
        await MobileService.SyncContext.InitializeAsync(store);
    }
}

编辑

我在想的一件事是,也许现在我必须确保在添加任何数据之前安装该软件的所有实例,但我想弄清楚这一点,以防将来添加更多安装。

编辑 2

我尝试手动从远程数据库中获取数据并将其导入到新安装中,但这会导致下次同步后项目在服务器上重复。

编辑 3

在添加任何数据之前安装多个实例也不起作用。两个实例都可以将数据推送到远程服务器,但都没有按应有的方式获取新数据。我希望它像某个地方的设置一样简单,但我正在运行的另一个 Azure 应用服务没有这个问题。

【问题讨论】:

    标签: c# azure synchronization


    【解决方案1】:

    问题最终是我试图使用旧的 NuGet 包 WindowsAzure.MobileServicesWindowsAzure.MobileServices.SQLiteStore。改用新的Microsoft.Azure.Mobile.ClientMicrosoft.Azure.Mobile.Client.SQLiteStore 包后一切正常。

    不同之处在于,新的 Azure 系统属性不再包含前导 __,因此旧包不知道如何比较服务器上的新数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 2013-12-03
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多