Azure移动服务 - IMobileServiceSyncTable PullAsync不会填充本地同步表

时间:2017-12-10 20:57:39

标签: azure xamarin.ios azure-mobile-services

我正在为我的Xamarin.iOS应用使用Azure移动服务。我在后端和客户端设置了我的App Service,并且我能够在我的应用程序上成功注册一个帐户,并在后端的相应表格中查看该条目。

当我尝试填充本地同步表时,使用对PullAsync的调用,同步表始终为空,即使我尝试返回该表中的所有记录,而不使用我的任何过滤器查询。

我不确定PullAsync为什么不填充我的同步表,即使没有例外。

以下是我的代码:

初始化本地商店

public class AzureMobileClientServiceDataManager : IAzureMobileClientServiceDataManager, IMobileServiceSyncHandler
{

    const string localDbPath = "sample.db";
    MobileServiceSQLiteStore store;

    MobileServiceClient client { get; set; }

    public AzureMobileClientServiceDataManager(IAzureMobileClientService azureMobileClientService)
    {
        CurrentPlatform.Init();

        SQLitePCL.CurrentPlatform.Init();

        //Initialize the Mobile service client with the Mobile App URL,Gatewaty URL and Key
        client = azureMobileClientService.GetMobileServiceClientInstance();
    }

    public async Task InitializeStoreAsync()
    {
        store = new MobileServiceSQLiteStore(localDbPath);
        store.DefineTable<Account>();
        store.DefineTable<UserProfile>();
        store.DefineTable<Purchase>();

        await client.SyncContext.InitializeAsync(store, this);

    }

    public MobileServiceClient GetInstance()
    {
        return client;
    }

    public Task OnPushCompleteAsync(MobileServicePushCompletionResult result)
    {
        foreach (var error in result.Errors)
        {
            Console.WriteLine("Error :" + error.RawResult);
        }

        return Task.FromResult(0);
    }

    public Task<JObject> ExecuteTableOperationAsync(IMobileServiceTableOperation operation)
    {
        return operation.ExecuteAsync();
    }
}

public interface IAzureMobileClientServiceDataManager
{
    Task InitializeStoreAsync();

    MobileServiceClient GetInstance();

}

在成功注册新用户之后,我调用此函数来填充表格。

public async Task<Result<bool>> PopulateData()
    {
       try{

       await accountTable.PullAsync("localAccount", accountTable.Where(ac => ac.Id == mobileServiceClient.CurrentUser.UserId));
       await userProfileTable.PullAsync("localUserProfile", userProfileTable.Where(up => up.AccountId == mobileServiceClient.CurrentUser.UserId));


      return Result<bool>.Success(true);

    }catch(Exception ex)
        {
            return Result<bool>.Failure(ex, ex.StackTrace);
        }

   }

1 个答案:

答案 0 :(得分:0)

每张桌子都有azuredatamanager吗?如果是这样,你会遇到问题。 MobileServiceClient意味着是全局的,并且您将遇到多个表的问题,因为您每次都要重新初始化表。

另外,尝试将记录SQLite存储添加到您的应用程序,这将记录所有本地数据库语句,这里有一个示例:https://github.com/Azure-Samples/app-service-mobile-dotnet-todo-list-files/blob/master/src/client/MobileAppsFilesSample/Helpers/LoggingHandler.cs