英文:
Data is not saved to Anonymous Parse User
问题
以下是翻译好的内容:
我是 Android 的新手,正在尝试开发一个 Uber 克隆应用。我正在将用户注册为匿名用户到 Parse Server。用户有两个选项,要么选择司机选项,要么选择乘客选项。当用户选择这两个选项之一时,我希望将这个数据(用户是乘客还是司机)保存到 Parse 用户对象中,以字符串形式,使用键“riderORdriver”。
我尝试将字符串保存到 Parse 用户对象的方式如下 -
String userType = "rider";
if(switchButton.isChecked())
{
userType = "driver";
}
ParseQuery<ParseUser> parseQuery = ParseUser.getQuery();
parseQuery.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
final String finalUserType = userType;
parseQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if(e == null && objects.size() > 0)
{
ParseUser user = objects.get(0);
user.put("riderORdriver", finalUserType);
user.saveInBackground();
}
}
});
当前用户始终是匿名用户,因为我已经关闭了自动用户启用,并且我正在使用 ParseAnonymousUtils 登录匿名用户。新的匿名用户出现在 Parse Dashboard 中的 User 类中,但列“riderORdriver”不会出现在其中。我还尝试了这个更简单的版本 -
ParseUser user = ParseUser.getCurrentUser();
user.put("riderORdriver", userType);
user.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e == null)
{
Log.i("Status", "用户已更新");
}
else
{
Log.i("Status", "用户未更新");
}
// 这两条消息都没有打印到日志中
}
});
但似乎这也不起作用。有人可以指出为什么会出现这种情况吗?
编辑 我是在按钮的点击函数中编写这段代码。这可能是出现问题的原因吗?
英文:
I am new to android and I am trying to develop an Uber Clone app. I am registering the user as an Anonymous User onto the Parse Server. I have two options for the User, to select either the driver option or the rider option. When the user selects one of these two options, I want to save this data(whether the user is a rider or a driver) to the Parse User object in the form of a String with key "riderORdriver".
This is how I am trying to save the string to the Parse User object -
String userType = "rider";
if(switchButton.isChecked())
{
userType = "driver";
}
ParseQuery<ParseUser> parseQuery = ParseUser.getQuery();
parseQuery.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
final String finalUserType = userType;
parseQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if(e == null && objects.size() > 0)
{
ParseUser user = objects.get(0);
user.put("riderORdriver", finalUserType);
user.saveInBackground();
}
}
});
The Current User is always an Anonymous User as I have turned off the enable automatic user and I am using ParseAnonymousUtils to log in the Anonymous User. The new Anonymous user appears in the User class in the Parse Dashboard, but the column "riderORdriver" does not appear in it. I have also tried the simpler version of this -
ParseUser User = ParseUser.getCurrentUser();
User.put("riderORdriver", userType);
User.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e == null)
{
Log.i("Status", "User Updated");
}
else
{
Log.i("Status", "User not updated");
}
//none of these two messages is being printed to the log
}
});
But that doesn't seem to work either. Can someone please point out the reason as to why this is happening?
Edit I am writing this code in the OnClick function of a button. Could that be the reason for this?
答案1
得分: 0
我仍然不知道问题出在哪里,但在“构建”部分点击“清理项目”对我有用。现在请求正在更新,显示“riderORdriver”字符串。我之前已经尝试过清理项目一次,但都是徒劳的。这次奏效了。也许这是解决这个问题的最佳方法。
英文:
I still don't know what the problem was, but clicking Clean Project in the Build Section did it for me. The request is now updating with the "riderORdriver" string. I had already tried to Clean the Project once, but all in vain. This time, it worked. Maybe that's the best bet at solving this issue.
专注分享java语言的经验与见解,让所有开发者获益!
评论