Azure移动客户端 - 脱机同步已删除数据

时间:2018-06-15 15:32:15

标签: azure azure-mobile-services

尝试更好地了解Mobile Sync Client如何处理来自数据库的已删除数据。

所以我从Sync表中提取了一个数据对象列表,如下所示:

public class AzureMobileService
{
    public MobileServiceClient Client { get; private set; }
    private IMobileServiceSyncTable<Debt> debtTable;

    public async Task Initialize()
    {
        if (Client != null)
        {
            return;
        }

        Client = new MobileServiceClient("https://ajhmobile.azurewebsites.net");
        var path = Path.Combine(MobileServiceClient.DefaultDatabasePath, "ajhmobile.db");
        var store = new MobileServiceSQLiteStore(path);
        store.DefineTable<Debt>();
        await Client.SyncContext.InitializeAsync(store, StoreTrackingOptions.NotifyLocalAndServerOperations);
        debtTable = Client.GetSyncTable<Debt>();
    }

    public async Task SyncAsync()
    {
        try
        {
            await Client.SyncContext.PushAsync();

            await debtTable.PullAsync(
                "all",
                this.debtTable.CreateQuery());
        }
        catch (MobileServicePushFailedException exc)
        {
            // handle resolve
        }
    }

    public async Task<List<Debt>> GetAllDebts()
    {
        await Initialize();
        await SyncAsync();
        return await debtTable.ToListAsync();
    }
}

但是,如果我从数据库中删除所有债务数据并运行同步刷新,它仍然会返回记录,就像它没有访问API并查找新数据一样。

有什么我做得不好或者只是不了解Sync表应该如何工作?

我想当我尝试同步数据时,如果我在本地有记录但是服务器不再有它们,它应该没有返回结果,对吗?

1 个答案:

答案 0 :(得分:1)

如果从数据库中完全删除记录,则还必须在调用PullAsync()提取新记录之前从设备上的表中清除数据。通常,您可以将记录保留在服务器上,但将deleted列设置为1。然后,当客户端提取更新时,它将看到这些记录已被标记为已删除。