Reputation: 43
So, I create an async connection on the app start which creates a "Profiles.db3" in the "data/user/0/com.your.mypackage/files" (I checked, it exists and is empty)
Everytime I try to register as a new user it always tells me there's an existing user
This is the method to check for an existing user:
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;
}
}
And I use it when creating a new profile:
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;
}
}
This method always returns false since CheckExistingUser returns true.
This is to initiate the connection
public async Task Init()
{
if (db != null)
return;
db = DependencyService.Get<ISQLiteInterface>().GetSQLiteAsyncConnection();
await db.CreateTableAsync<Profile>();
}
I call it in the SignUp view model:
if (await _profileService.NewProfile((User)Profile))
{
App.Current.MainPage = new AppShell();
}
I am new to all this stuff so I don't see where's the problem since the logic seems correct to me.
Upvotes: 1
Views: 109
Reputation: 4376
Just change your code to:
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;
}
}
The reason is always not null is because the call returns a task.
You need to await that task instead of just checking if the task is null or not.
You see you are calling FirstOrDefaultAsync which returns a task.
Upvotes: 0