Azure 移动服务:仅在本地定义 Table
Azure mobile service: Define a Table only locally
我在 Xamarin 应用程序中使用离线同步。我有以下情况:
我有几个 table 可以很好地同步,还有一个 table 叫做 LocalOnlyTable
我不想同步。我只想 read/write 在本地。
当我像这样拉出我的 table 之一时出现问题:
await exerciseTable.PullAsync(string.Format("{0}ItemByFK", typeof(Exercise).Name), exerciseTable.CreateQuery());
我得到 MobileServicePushFailedException
说 404 LocalOnlyTable does not exist
。
我想知道为什么移动服务试图 push/pull LocalOnlyTable
和
如何防止移动服务尝试同步 LocalOnlyTable
?
跟踪所有使用 MSSyncTable API 执行的操作以发送到服务器。如果您 table 不想跟踪,则不应使用 MSSyncTable API 来 insert/update 记录。
您应该能够使用 SQLiteStore 方法(如 upsert)或在您的 SQLite Db 上直接为您未跟踪的 table 执行 SQL .
刚在这里遇到你的问题,想分享我的解决方案。
1) 创建自定义 TableSyncHandler 以阻止仅限本地的表:
public class TableSyncHandler : IMobileServiceSyncHandler
{
private readonly IMobileServiceClient _client;
private readonly HashSet<string> _excludedTables = new HashSet<string>();
public TableSyncHandler(IMobileServiceClient client)
{
_client = client;
}
public void Exclude<T>()
{
_excludedTables.Add(_client.SerializerSettings.ContractResolver.ResolveTableName(typeof(T)));
}
public Task OnPushCompleteAsync(MobileServicePushCompletionResult result)
{
return Task.FromResult(0);
}
public Task<JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation)
{
if (_excludedTables.Contains(operation.Table.TableName))
{
return Task.FromResult((JObject) null);
}
return operation.ExecuteAsync();
}
}
2) 在初始化MobileServiceClient的SyncContext时,将需要排除的表注册到这个syncHandler中,然后使用syncHandler初始化SyncContext:
_store = new MobileServiceSQLiteStore("YourStore");
_store.DefineTable<User>();
_store.DefineTable<LocalOnlyTable>();
_syncHandler = new TableSyncHandler(client);
// LocalOnlyTable is excluded from sync operations
_syncHandler.Exclude<LocalOnlyTable>();
await client.SyncContext.InitializeAsync(_store, _syncHandler);
免责声明:
- 这还没有投入生产,所以我不知道是否会对性能产生影响,但目前在测试中似乎运行良好。
- 此解决方案基于 Azure 移动服务客户端 v1.3.2 源代码。当 synchandler returns 为空结果时,它没有做任何事情 (pull/push)。这种行为将来可能会改变。
我在 Xamarin 应用程序中使用离线同步。我有以下情况:
我有几个 table 可以很好地同步,还有一个 table 叫做 LocalOnlyTable
我不想同步。我只想 read/write 在本地。
当我像这样拉出我的 table 之一时出现问题:
await exerciseTable.PullAsync(string.Format("{0}ItemByFK", typeof(Exercise).Name), exerciseTable.CreateQuery());
我得到 MobileServicePushFailedException
说 404 LocalOnlyTable does not exist
。
我想知道为什么移动服务试图 push/pull LocalOnlyTable
和
如何防止移动服务尝试同步 LocalOnlyTable
?
跟踪所有使用 MSSyncTable API 执行的操作以发送到服务器。如果您 table 不想跟踪,则不应使用 MSSyncTable API 来 insert/update 记录。
您应该能够使用 SQLiteStore 方法(如 upsert)或在您的 SQLite Db 上直接为您未跟踪的 table 执行 SQL .
刚在这里遇到你的问题,想分享我的解决方案。
1) 创建自定义 TableSyncHandler 以阻止仅限本地的表:
public class TableSyncHandler : IMobileServiceSyncHandler
{
private readonly IMobileServiceClient _client;
private readonly HashSet<string> _excludedTables = new HashSet<string>();
public TableSyncHandler(IMobileServiceClient client)
{
_client = client;
}
public void Exclude<T>()
{
_excludedTables.Add(_client.SerializerSettings.ContractResolver.ResolveTableName(typeof(T)));
}
public Task OnPushCompleteAsync(MobileServicePushCompletionResult result)
{
return Task.FromResult(0);
}
public Task<JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation)
{
if (_excludedTables.Contains(operation.Table.TableName))
{
return Task.FromResult((JObject) null);
}
return operation.ExecuteAsync();
}
}
2) 在初始化MobileServiceClient的SyncContext时,将需要排除的表注册到这个syncHandler中,然后使用syncHandler初始化SyncContext:
_store = new MobileServiceSQLiteStore("YourStore");
_store.DefineTable<User>();
_store.DefineTable<LocalOnlyTable>();
_syncHandler = new TableSyncHandler(client);
// LocalOnlyTable is excluded from sync operations
_syncHandler.Exclude<LocalOnlyTable>();
await client.SyncContext.InitializeAsync(_store, _syncHandler);
免责声明:
- 这还没有投入生产,所以我不知道是否会对性能产生影响,但目前在测试中似乎运行良好。
- 此解决方案基于 Azure 移动服务客户端 v1.3.2 源代码。当 synchandler returns 为空结果时,它没有做任何事情 (pull/push)。这种行为将来可能会改变。