Search code examples
c#xamarinasync-awaitkinvey

No Async method working


My complete Kinvey and Xamarin Async-API is not working with await/async methods. I dont know where the problem is. I didn't find any error in my code, but the general methods I use should be ok, like

User _User= await KinveyXamarin._Client.User().LoginAsync();

I searched on the Internet, but I didn't find any similar problems. It returns the Compile_time Error

Error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

I really dont know whats causing this problem, these are nearly my first steps with asynchronous API and Kinvey. Any help would be greatly appreciated.


Solution

  • The method in which you write await should be an async method itself.

    public async Task MyMethodAsync()
    {
        User _User = await KinveyXamarin._Client.User().LoginAsync();
        // more code
    }
    

    If you want to return an object, use Task<T>:

    public async Task<User> MyMethodAsync()
    {
        User _User = await KinveyXamarin._Client.User().LoginAsync();
        // more code
    
        return _User;
    }
    

    Extra note: please try to avoid async void, it's evil.

    Edit: it's a best practice to end your async methods on -Async to make this clear to anyone using your code.