Azure Easy Tables init(?)在应用程序的一部分上失败但在另一部分上成功。发生了什么事?

时间:2017-09-20 07:48:39

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

我正在开发一款Xamarin.Android应用。我正在连接到Azure移动应用程序,我正在EasyTable中获取/存储数据。

现在这是我的用户'登录'和注册的代码。这只是测试查询,所以不要介意明显不安全的登录:

public async Task Initialize()
        {
            MobileService = new MobileServiceClient(myappwebsiteinazurestring);

            string path = Path.Combine(MobileServiceClient.DefaultDatabasePath, "UserSync.db");
            var store = new MobileServiceSQLiteStore(path);
            store.DefineTable<Users_Table>();
            await MobileService.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
            userTable = MobileService.GetSyncTable<Users_Table>();
        }

        public async Task SyncUsers()
        {
            await userTable.PullAsync("Users_Table", userTable.CreateQuery());
            await MobileService.SyncContext.PushAsync();
        }

        public async Task<List<Users_Table>> loginUser(string username, string password)
        {
            List<Users_Table> userLogin = await userTable
                .Where(user => user.Username == username && user.Password == password)
                .ToListAsync();
            await MobileService.SyncContext.PushAsync();
            return userLogin;
        }

这是登录和初始化。现在这里是注册部分:

 public async Task AddUser(Users_Table user)
        {
            var InsUser = new Users_Table
            {
                Name = user.Name,
                Username = user.Username,
                Password = user.Password,
                LicensePlate = user.LicensePlate,
                Email = user.Email,
                Exp = user.Exp
            };

            await userTable.InsertAsync(InsUser);
            await SyncUsers();

        }

现在这很好。这是我从我的应用程序中调用它们的方式:

 private async void LoginButton_Click(object sender, EventArgs e)
        {

            EditText username = FindViewById<EditText>(Resource.Id.LoginEditTextUsername);
            EditText password = FindViewById<EditText>(Resource.Id.LoginEditTextPassword);

            var prog = new ProgressDialog(this);
            prog.SetMessage("Logging in...");
            prog.Show();
            AzureDataService az = new AzureDataService();
            List<Users_Table> user = new List<Users_Table>();

            try
            {
                await az.Initialize();
                user = await az.loginUser(username.Text, password.Text);
            }
            catch (Exception ex)
            {

                Toast.MakeText(this.ApplicationContext, "Error: " + ex.Message + "\n" + ex.Source, ToastLength.Long).Show();
                return;
            }

注册:

 private async void CreateProfile()
        {
            EditText regUsername = FindViewById<EditText>(Resource.Id.RegisterLayoutUsername);
            EditText regPassword = FindViewById<EditText>(Resource.Id.RegisterLayoutPassword);
            EditText regConfirmPassword = FindViewById<EditText>(Resource.Id.RegisterLayoutConfirmPassword);
            EditText regPlateNumber = FindViewById<EditText>(Resource.Id.RegisterLayoutPlateNumber);
            EditText regEmail = FindViewById<EditText>(Resource.Id.RegisterLayoutEmail);
            EditText regName = FindViewById<EditText>(Resource.Id.RegisterLayoutName);

            var user = new Users_Table
            {
                Username = regUsername.Text,
                Password = regPassword.Text,
                LicensePlate = parseLicensePlate(regPlateNumber.Text),
                Name = regName.Text,
                Email = regEmail.Text,
                Exp = 0
            };

            try
            {
                var pd = new ProgressDialog(this);
                pd.SetMessage("Creating Profile...");
                pd.Show();
                AzureDataService az = new AzureDataService();
                await az.Initialize();
                await az.AddUser(user);
                pd.Dismiss();
            }

一旦我在任何带有apk的设备上进行测试,登录就会失败。该应用程序收到null Users_Table,因此显示用户名/密码不正确的消息。

然而奇怪的是:寄存器没有任何问题。一旦我成功注册了新用户,对于现有用户和新创建的用户,登录也会通过。这就像在register方法中发生的事情一样,可以启动与任何事物的连接。

有没有人经历过这个?发生了什么事?

1 个答案:

答案 0 :(得分:0)

我通过在任何进程之前添加syncusers()来实现它的工作,例如这样登录:

    public async Task<List<Users_Table>> loginUser(string username, string password)
    {



        await SyncUsers();//here
        List<Users_Table> userLogin = await userTable
            .Where(user => user.Username == username && user.Password == password)
            .ToListAsync();
        await MobileService.SyncContext.PushAsync();
        return userLogin;
    }