Skip to content

User.current not returning all variables #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vdkdamian opened this issue Dec 19, 2020 · 6 comments · Fixed by #38
Closed

User.current not returning all variables #37

vdkdamian opened this issue Dec 19, 2020 · 6 comments · Fixed by #38
Labels
type:feature New feature or improvement of existing feature

Comments

@vdkdamian
Copy link
Contributor

When a user is logged in, and you request a custom key from the user, it returns nil even though there is a value for that user in the database.

This is my ParseUser structure

struct User: ParseUser {
            //: These are required for ParseObject
            var objectId: String?
            var createdAt: Date?
            var updatedAt: Date?
            var ACL: ParseACL?

            //: These are required for ParseUser
            var username: String?
            var email: String?
            var emailVerified: Bool?
         
            //: Custom
            var userId: String?
 }

This is what I call on launch

let currentUser = User.current
        print("currentUser - objectId: \(String(describing: currentUser?.objectId))")
        print("currentUser - createdAt: \(String(describing: currentUser?.createdAt))")
        print("currentUser - updatedAt: \(String(describing: currentUser?.updatedAt))")
        print("currentUser - ACL: \(String(describing: currentUser?.ACL))")
        
        print("currentUser - username: \(String(describing: currentUser?.username))")
        print("currentUser - email: \(String(describing: currentUser?.email))")
        print("currentUser - emailVerified: \(String(describing: currentUser?.emailVerified))")
        //Custom key
        print("currentUser - userId: \(String(describing: currentUser?.userId))")

This is the log
Schermafbeelding 2020-12-20 om 00 40 06

This is an example of 3 users in Parse database
Schermafbeelding 2020-12-20 om 00 46 07

@cbaker6
Copy link
Contributor

cbaker6 commented Dec 20, 2020

Are you setting those custom keys from either the CloudCode or the Dashboard? If so, you need to fetch the data from your server because your local user doesn't have it and has no way to know it's been updated. What happens when you try?:

let updatedUser = try User.current?.fetch()
print(updatedUser.emailVerified)
print(updatedUser.userId)

If you are saving these custom values from the device itself and not seeing the values after, something else is going on. I don't see this being the case because this works in playgrounds

struct User: ParseUser {
//: These are required for ParseObject
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var ACL: ParseACL?
//: These are required for ParseUser
var username: String?
var email: String?
var password: String?
//: Your custom keys
var customKey: String?
}
/*: Save your first customKey value to your ParseUser
Asynchrounously - Performs work on background
queue and returns to designated on designated callbackQueue.
If no callbackQueue is specified it returns to main queue.
*/
User.current?.customKey = "myCustom"
User.current?.save { results in
switch results {
case .success(let updatedUser):
print("Succesufully save myCustomKey to ParseServer: \(updatedUser)")
case .failure(let error):
assertionFailure("Failed to update user: \(error)")
}
}

Also, make sure you have the latest version of Parse-Swift, there was recently (12/17/20) a fix to fetch.

@cbaker6
Copy link
Contributor

cbaker6 commented Dec 20, 2020

Update, I need to look into fetch on the current user, but I suspect you are changing the custom keys from the server as I mentioned above. In order to get that info and save it to your current user you can do the following:

var findUser = User.query()
findUser.where("objectId" == User.current!.objectId!) //Force unwrapping to be concise, but you should guard agains these

findUser.first(callbackQueue: .main) {
    result in
    switch result {
    case .success(let updatedUser):
        print("Successfully fetched latest user from ParseServer: \(updatedUser)")
        User.current = updatedUser
        print(User.current!.emailVerified!) //Will contain the latest value from the server
        print(User.current!.userId!) //Will contain the latest value from the server
    case .failure(let error):
        print("Failed to update user: \(error)")
    }
}

@cbaker6
Copy link
Contributor

cbaker6 commented Dec 20, 2020

After #38 is merged, you will be able to fetch User.current or Installation.current like I mentioned earlier:

//Synchronous
let _ = try User.current?.fetch()
print(User.current?.emailVerified)
print(User.current?.userId)

//Asynchronous
User.current?.fetch { result in
    switch result {
    case .success(_):
        print("Successfully fetched updated user from ParseServer: \(User.current!)")
    case .failure(let error):
        print("Failed to fetch user: \(error)")
    }
}

@vdkdamian
Copy link
Contributor Author

vdkdamian commented Dec 20, 2020

@cbaker6 I am sorry, I indeed didn't fetch the user. I thought this happend in the background code from ParseSwift.

I now tried using your fetch code:

User.current?.fetch { result in
    switch result {
    case .success(_):
        print("Successfully fetched updated user from ParseServer: \(User.current!)")
    case .failure(let error):
        print("Failed to fetch user: \(error)")
    }
}

But I guess I need to wait for the merge since this didn't work.

@cbaker6
Copy link
Contributor

cbaker6 commented Dec 20, 2020

You should use the query block of code until the merge:

var findUser = User.query()
findUser.where("objectId" == User.current!.objectId!) //Force unwrapping to be concise, but you should guard agains these

findUser.first(callbackQueue: .main) {
    result in
    switch result {
    case .success(let updatedUser):
        print("Successfully fetched latest user from ParseServer: \(updatedUser)")
        User.current = updatedUser
        print(User.current!.emailVerified!) //Will contain the latest value from the server
        print(User.current!.userId!) //Will contain the latest value from the server
    case .failure(let error):
        print("Failed to update user: \(error)")
    }
}

@mtrezza mtrezza added type:feature New feature or improvement of existing feature and removed type:improvement labels Dec 6, 2021
@parse-github-assistant
Copy link

The label type:feature cannot be used in combination with type:improvement.

@parse-github-assistant parse-github-assistant bot removed the type:feature New feature or improvement of existing feature label Dec 6, 2021
@mtrezza mtrezza added the type:feature New feature or improvement of existing feature label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants