代码之家  ›  专栏  ›  技术社区  ›  spoooooooonman

SQLite.NET-PCL-当我检查现有用户时,即使数据库表为空,函数也总是返回true

  •  1
  • spoooooooonman  · 技术社区  · 2 年前

    因此,我在应用程序启动上创建了一个异步连接,该连接在“data/user/0/com.your.mypackage/files”中创建了“Profiles.db3”(我检查了一下,它存在并且为空)

    每次我尝试注册为新用户时,它总是告诉我有一个现有用户

    这是检查现有用户的方法:

    public async Task<bool> CheckExistingUser(Profile profile)
            {
                await Init();
                var d1 = db.Table<Profile>().Where(x => x.Username == profile.Username || x.Email == profile.Email).FirstOrDefaultAsync(); 
                if (d1 != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    

    我在创建新配置文件时使用它:

    public async Task<bool> NewProfile(User profile, string picture = "user.png")
            {
                await Init();
                if ((await CheckExistingUser(profile)))
                {
                    return false;
                }
                else
                {
                    Profile _profile = new Profile
                    {
                        Username = profile.Username,
                        Email = profile.Email,
                        Password = profile.Password,
                        Picture = picture,
                        ProfileType = ProfileType.User,
                        AdditionalData = JsonConvert.SerializeObject(new
                        {
                            profile.BorrowedBooks,
                            profile.Wishlist,
                            profile.ReturnedBooks
                        })
                    };
                    await db.InsertAsync(_profile);
                    return true;
                }
            }
    

    由于CheckExistingUser返回true,因此此方法始终返回false。

    这是为了启动连接

    public async Task Init()
            {
            
                if (db != null)
                    return;
    
    
                    db = DependencyService.Get<ISQLiteInterface>().GetSQLiteAsyncConnection();
                    await db.CreateTableAsync<Profile>();
            }
                
    

    我在SignUp视图模型中称之为:

     if (await _profileService.NewProfile((User)Profile))
                {
                    App.Current.MainPage = new AppShell();
                }
    

    我对这一切都是新手,所以我不知道问题出在哪里,因为逻辑对我来说似乎是正确的。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Jonathan Alfaro    2 年前

    只需将代码更改为:

    public async Task<bool> CheckExistingUser(Profile profile)
            {
                await Init();
               //use the await keyword here... otherwise you get a task object
                var d1 = await db.Table<Profile>().Where(x => x.Username == profile.Username || x.Email == profile.Email).FirstOrDefaultAsync(); 
                if (d1 != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    

    之所以总是不为null,是因为调用返回了一个任务。

    您需要等待该任务,而不是只检查该任务是否为null。

    你看你在打电话 第一个或默认异步 返回一个任务。

    推荐文章