I'm using parse.com Android sdk, in the parseUser I have added a column pointer to other data class named pointer. I access this data by:
ParseUser user=ParseUser.getCurrentUser();
ParseObject pointer= user.getParseObject("pointer");
pointer.fetchIfNeededInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
}
}
But I have a problem when I try to logout before doing this fetch. I logout by:
ParseUser.logOut();
and when this function is invoked the the app get locked.
You simply need to wrap this call to fetch in a check to see if there is still a logged in user:
ParseUser user = ParseUser.getCurrentUser();
if (user != null) {
// rest of your code here, as we have a currently logged in user
}
Related
I have the following code that allows users to update their data:
String username = "Any username that is currently already used by another user";
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
currentUser.setUsername(username);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// All good!
} else {
// Error: Account already exists for this username.
Log.e("Error: ", e.getMessage());
}
}
}
});
When the user is trying to update the username with one that is already used by someone else, it throws an error:
Account already exists for this username.
Which is exactly what I'd want it to, but when the user goes back (without making another request to change the username to one that is available) the ParseUser.get("username") returns the value that wasn't saved because it already exists (instead of the real value stored at the moment in the server).
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.fetchInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException exception) {
if (exception == null) {
// username now returns ("Any username that is currently already used by another user").
String username = user.get("username").toString().trim();
} else {
// Error
}
}
});
The only way I've found so far to fix the issue is by uninstalling/reinstalling the app. What's causing this? And is there any good way to fix this?
You should backup the current username before setUsername(newName).
if exception happened you have two options either :
Re fetch the user object by ParseUser.getCurrentUser().fetch() (to restore the object to his previous state)
Re set the username to the previous value(that you already back up before)
Why this happened ?
because when you called the setUsername() of the user object you changed your local copy of the object (but you didn't sync it yet with the server) and then when you made save() and the save operation failed the local copy still has the latest changes that you made locally
Followed by Parse doc, I created an anonymous user while app is first launched.
...
Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);
ParseUser.enableRevocableSessionInBackground();
if (ParseUser.getCurrentUser() == null) {
ParseUser.enableAutomaticUser();
ParseUser.getCurrentUser().increment("RunCount");
ParseUser.getCurrentUser().saveInBackground();
}
In the parse core, it shows that a new "User" and "Session" row is successfully added.
...
ParseUser.getCurrentUser().setUsername("Leo");
ParseUser.getCurrentUser().setPassword("sasami");
ParseUser.getCurrentUser().signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
...
}
}
...
Then after I calling the "signUpInBackground" method, I found the just-added User row has been updated, but the just-added Session row was disappear. And consequently the user-related query and execution always returns an
com.parse.ParseRequest$ParseRequestException: invalid session token
P.S.
It doesn't matter if signup successfully or returned error. The Session row disappears anyway.
I'm using Parse as my backend and I'm trying to "like" a post that another user posted on the app. I'm querying to get the post, then incrementing the number of likes by 1, then adding the current user's object ID to an array that holds all the ID's of users which liked the post.
carLikeQuery.getInBackground(carItem.getObjectId(), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
object.increment("likes");
object.addUnique("usersWhoLike", ParseUser.getCurrentUser().getObjectId());
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if(e==null) {
Log.d("SAVE", "Like saved :)");
} else {
Log.e("SAVE", "Not saved :( :" + e.getLocalizedMessage());
}
}
});
}
});
The error I'm getting:
E/SAVE: Not saved :( :java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
I saw the source code for the ParseUser from somewhere:
void validateSave() {
if (getObjectId() == null) {
throw new IllegalArgumentException("Cannot save a ParseUser until it has been signed up. Call signUp first.");
}
if (!isAuthenticated() && isDirty()) {
throw new IllegalArgumentException("Cannot save a ParseUser that is not authenticated.");
}
}
Doing the same kind of checking in my code reveals that the the currentUser is AUTHENTICATED and NOT DIRTY.
What could the issue be? To be honest, I want to say that it was working just fine before today, but obviously I was changing something and made a mistake down the line and I can't find it! Any help would be greatly appreciated.
I resolved the issue by creating an entirely new Parse application with the same data structure/layout. It just plain worked without any code changes.
You can follow the issue on GitHub here
I want to update a field from table 'User' of a ParseUser different by the one logged in.
The code below is not working and I was wondering if there is some impediment that does not allow to update other's user info.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereMatches("objectId", "4mdsJKKgDJ");
userQuery.getFirstInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser parseUser, ParseException e) {
// I retreive the parseUser object correctly here
int score = parseUser.getInt("score"); // I have added this column in User table with a default value of 0
parseUser.put("score", score+1);
parseUser.saveInBackground(); // nothing happens. the value is not updated
}
});
Thanks.
I answer myself to my question: you cannot update another user info, different by the one logged in.
Using:
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("ParseException: "+ e);
}
});
revealed the problem:
com.parse.ParseException: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
So I will need to use another table to keep this info.
I ran into some strange behaviour while implementing the facebook login from Parse.com in my application. Here's how to replicate it with the Blank Facebook App sample code from their samples:
I want to create a new "Post" associated to the current user. At the end of the onCreate() from UserDetailsActivity, I added :
ParseObject post = new ParseObject("Post");
post.put("name", "New post");
post.put("user", ParseUser.getCurrentUser());
post.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
e.printStackTrace();
}
}
});
This work pretty well until I enable the localDataStore in the Application. The code above returns :
com.parse.ParseException : java.lang.IllegalArgumentException : Cannot save a ParseUser until it has been signed up. Call signUp first.
Note : the data associated with the ParseUser returned in the callback of ParseFacebookUtils.logIn() are null (objectId, username, ..) which explains the behaviour above.
The question is then : why adding
Parse.enableLocalDatastore(this);
to the App makes facebook login return an empty ParseUser ?
Ths error happens once every two launches :
First launch, no valid current user is stored in the offlineStore database, facebook login successfully add one, everything works perfectly
Second launch, a valid user is already stored, which is replaced by an "OfflineObject" User with no objectId or data, causing the error above.
I'm using Parse 1.5.0 & Facebook SDK 3.14.
It is very possible that ParseUser.getCurrentUser() is returning null as per https://parse.com/docs/android/api/com/parse/ParseUser.html#getCurrentUser(). You may want to do a facebook login first and then try to use the ParseUser
ParseFacebookUtils.logIn(getActivity(), new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if(user != null) {
// use ParseUser here
}
}
});
Also, I wouldn't recommend using Parse 1.5.0 with version 3.14 of the facebook SDK. The docs for Parse https://www.parse.com/docs/android_guide#fbusers state that "Parse is compatible with v3.0 of the Facebook SDK for Android." My experience leads me to believe they aren't lying.