user1070827
user1070827

Reputation: 123

C# how to deal with callback function

  public User Login(User user)
    {
        User responseUser = null; ;
        parse.Users.Login<User>("hello", "99999", r =>
            {
                if (r.Success) { responseUser = r.Data; }
            });
        return responseUser;
    }

Why return responseUser is null but r.Data is not null? Thank you!!!

Upvotes: 0

Views: 238

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038880

You haven't told us how the parse.Users.Login<User> function works but it seems that it is asynchronous. This means that it will return immediately and the callback called much later. So you will return null from the main function.

You need to modify the Login function so that it works with a callback as well:

public void Login(User user, Action<User> action)
{
    User responseUser = null;
    parse.Users.Login<User>("hello", "99999", r =>
    {
        if (r.Success) 
        { 
            action(r.Data);
        }
        else
        {
            action(null);
        }
    });
}

Upvotes: 1

Related Questions