Search code examples
c#asp.netasp.net-mvcparse-platformparse-dotnet-sdk

How can I query a ParseObject collection asynchronously in my ASP.NET MVC app?


I have tried the following. Currently, I'm just trying to read the collection of objects queried in the console. According to the guide, asynchronous queries are preferred: http://docs.parseplatform.org/dotnet/guide/#queries

public class IndexModel : PageModel
    {
        public void OnGet()
        {
            Task<int> task = HandleMessagesAsync();
            task.Start();

        }

        private async Task<int> HandleMessagesAsync()
        {
            var query = ParseObject.GetQuery("Message");
            IEnumerable<ParseObject> results = await query.FindAsync();

            Console.WriteLine(results);

            throw new NotImplementedException();
        }
    }

I get the following error:

System.InvalidOperationException: 'Start may not be called on a promise-style task.'

Solution

  • The Task class starts automatically, as per this guide: https://social.msdn.microsoft.com/Forums/vstudio/en-US/70f82b79-188e-4e91-8f86-b5a9382663fb/problem-with-taskstart?forum=netfxbcl

    There is no need to invoke .Start();

    public class IndexModel : PageModel
        {
            public void OnGet()
            {
                Task<int> task = HandleMessagesAsync();
            }
    
            private async Task<int> HandleMessagesAsync()
            {
                var query = ParseObject.GetQuery("Message");
                IEnumerable<ParseObject> results = await query.FindAsync();
                var count = await query.CountAsync();
    
                Debug.WriteLine("# Message: " + count);
    
                throw new NotImplementedException();
            }
        }
    

    I used Debug.WriteLine("# Message: " + count); to make sure that the correct number of Message objects were queried from my database.