{"action":"created","issue":{"url":"https://api.github.com/repos/parse-community/Parse-SDK-Android/issues/850","repository_url":"https://api.github.com/repos/parse-community/Parse-SDK-Android","labels_url":"https://api.github.com/repos/parse-community/Parse-SDK-Android/issues/850/labels{/name}","comments_url":"https://api.github.com/repos/parse-community/Parse-SDK-Android/issues/850/comments","events_url":"https://api.github.com/repos/parse-community/Parse-SDK-Android/issues/850/events","html_url":"https://github.com/parse-community/Parse-SDK-Android/issues/850","id":339289036,"node_id":"MDU6SXNzdWUzMzkyODkwMzY=","number":850,"title":"ParseUser user.saveEventually() always create new user in database","user":{"login":"andj207","id":3950215,"node_id":"MDQ6VXNlcjM5NTAyMTU=","avatar_url":"https://avatars3.githubusercontent.com/u/3950215?v=4","gravatar_id":"","url":"https://api.github.com/users/andj207","html_url":"https://github.com/andj207","followers_url":"https://api.github.com/users/andj207/followers","following_url":"https://api.github.com/users/andj207/following{/other_user}","gists_url":"https://api.github.com/users/andj207/gists{/gist_id}","starred_url":"https://api.github.com/users/andj207/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andj207/subscriptions","organizations_url":"https://api.github.com/users/andj207/orgs","repos_url":"https://api.github.com/users/andj207/repos","events_url":"https://api.github.com/users/andj207/events{/privacy}","received_events_url":"https://api.github.com/users/andj207/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-07-09T02:56:44Z","updated_at":"2018-09-18T09:58:14Z","closed_at":null,"author_association":"NONE","body":"ParseUser was newly created every time  I use user.saveEventually(). I checked in the ParseDashboard, the Installation object for my device is unique but the \"user\" field in Installation table always different every time I completely close the app (back pressed, wipe out recently apps) and reopen it. The User table has new row too.\r\n\r\nMy code:\r\n\r\n```java\r\nParseUser.enableAutomaticUser();\r\n        ParseUser user = ParseUser.getCurrentUser();\r\n        if (user != null) {\r\n            ParseACL.setDefaultACL(new ParseACL(ParseUser.getCurrentUser()), true);\r\n            if (user.get(\"appLaunch\") == null) {\r\n                user.put(\"appLaunch\", 0);\r\n                user.put(\"notifyAll\", true);\r\n            }\r\n            user.saveEventually();  // <-- this line will create new user everytime it run\r\n        }\r\n\r\nParseInstallation installation = ParseInstallation.getCurrentInstallation();\r\n        if (installation != null) {\r\n            String refreshedToken = FirebaseInstanceId.getInstance().getToken();\r\n            if (refreshedToken != null) {\r\n                installation.setDeviceToken(refreshedToken);\r\n            }\r\n            installation.put(\"user\", ParseUser.getCurrentUser());\r\n            installation.saveEventually();\r\n        }\r\n```\r\n\r\nI use latest ParseServer and: \r\n    implementation \"com.parse:parse-android:1.17.3\""},"comment":{"url":"https://api.github.com/repos/parse-community/Parse-SDK-Android/issues/comments/422332087","html_url":"https://github.com/parse-community/Parse-SDK-Android/issues/850#issuecomment-422332087","issue_url":"https://api.github.com/repos/parse-community/Parse-SDK-Android/issues/850","id":422332087,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMjMzMjA4Nw==","user":{"login":"nisrulz","id":2096087,"node_id":"MDQ6VXNlcjIwOTYwODc=","avatar_url":"https://avatars3.githubusercontent.com/u/2096087?v=4","gravatar_id":"","url":"https://api.github.com/users/nisrulz","html_url":"https://github.com/nisrulz","followers_url":"https://api.github.com/users/nisrulz/followers","following_url":"https://api.github.com/users/nisrulz/following{/other_user}","gists_url":"https://api.github.com/users/nisrulz/gists{/gist_id}","starred_url":"https://api.github.com/users/nisrulz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nisrulz/subscriptions","organizations_url":"https://api.github.com/users/nisrulz/orgs","repos_url":"https://api.github.com/users/nisrulz/repos","events_url":"https://api.github.com/users/nisrulz/events{/privacy}","received_events_url":"https://api.github.com/users/nisrulz/received_events","type":"User","site_admin":false},"created_at":"2018-09-18T09:58:14Z","updated_at":"2018-09-18T09:58:14Z","author_association":"NONE","body":"So, the issue is that the ParseAnonymousUser is actually not getting saved properly, which results in no user being available when the app starts again. So the usual path of creating a new anon user is followed.  The solution that worked for me was like below:\r\n\r\nIn my Application file\r\n```java\r\n@Override \r\npublic void onCreate() {\r\n     ...\r\n     // Enable automatic creation of ParseAnonymousUser \r\n     ParseUser.enableAutomaticUser();\r\n    \r\n     // Login the anonymous user so as to create an entry in the Parse's User table\r\n     loginAnonUserIfNeeded() \r\n}\r\n``` \r\nwhere `loginAnonUserIfNeeded()` is another method, which takes care of logging in the ParseAnonymousUser.\r\n\r\n```java\r\nvoid loginAnonUserIfNeeded() {\r\n        // If the current user does not have the object id then it is\r\n        // an anon user, then go ahead and login the user into parse\r\n        if (ParseUser.getCurrentUser() != null\r\n                && ParseUser.getCurrentUser().getObjectId() == null) {\r\n            // call to login only creates the anon user but does not persist it\r\n            ParseAnonymousUtils.logIn((user, e) -> {\r\n                if (e == null) {\r\n                    // The user returned here is ParseUser object which is now stored in remote Parse\r\n                    // server. If the login was not successful this will be null. So we check for the\r\n                    // user object that si returned is not null and login was successful.\r\n                    if (user != null) {\r\n                        // After login, save is required so that it persists across app starts\r\n                        // ⚠️ WARNING: Do not use saveEventually as it is buggy and creates new anon user\r\n                        // every time app is opened\r\n                        user.saveInBackground(e1 -> {\r\n                            if (e1 == null) {\r\n                                //User was saved successfully\r\n                            } else {\r\n                                // Print stacktrace of the error\r\n                                e1.printStackTrace();\r\n                            }\r\n\r\n                        });\r\n                    }\r\n                } else {\r\n                    // Print stacktrace of the error\r\n                    e.printStackTrace();\r\n                }\r\n            });\r\n        }\r\n    }\r\n```\r\n\r\nHow to test this: Setup OkHttpInterceptor for logging outgoing calls to Parse Server and you can validate that the create user call is made only once.\r\n\r\nI hope this helps others who bump into this problem in the future 😄 "}}
{"actor":{"display_login":"nisrulz"}}