Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Task is getting locked while retreiving data in viewmodel

$
0
0

Hi my fellow developers.
I have been working on a app for a couple of months now but recently when
we started our testing phase i've been noticing some strange task lockups when loading
and transforming my data.

This is my viewmodel:

`
namespace Doshi.Dojo.ViewModels
{
public class AdviceViewModel : ViewModelBase
{
private NotifyTaskCompletion<ObservableCollection> _advisors;
public NotifyTaskCompletion<ObservableCollection> Advisors
{
get => _advisors;
set
{
SetProperty(ref _advisors, value);
}
}

    private readonly IContactsService _contactsService;
    private readonly IAdvisorService _adviserService;
    private readonly ITipService _tipService;
    private IUserService _userService;
    private IStatusService _statusService;

    public AdviceViewModel(INavigationService navigationService, IContactsService contactsService,
        IAdvisorService adviserService, ITipService tipService,
        IUserService userService, IUserDialogs userDialogs,
        IStatusService statusService) : base(navigationService)
    {
        _contactsService = contactsService;
        _adviserService = adviserService;
        _tipService = tipService;
        _userService = userService;
        _statusService = statusService;

        Advisors = new NotifyTaskCompletion<ObservableCollection<AdvisorModel>>(LoadAdvisorsAsync());
    }

    private async Task<ObservableCollection<AdvisorModel>> LoadAdvisorsAsync()
    {
        var contact_ids = await _contactsService.GetContactsIdsAsync();

        var advisorTasks = contact_ids.Select(i => CreateAdvisorModelAsync(i));
        var advisors = await Task.WhenAll(advisorTasks);

        var tipTasks = advisors.Select(a => FetchTipsAsync(a));
        advisors = await Task.WhenAll(tipTasks);

//STOPS HERE

        return new ObservableCollection<AdvisorModel>(advisors.ToList());
    }

    private async Task<AdvisorModel> CreateAdvisorModelAsync(string id)
    {
        var advisor_profile = await _adviserService.GetAdviserProfileAsync(id).ConfigureAwait(false);

        return new AdvisorModel
        {
            Website = advisor_profile.Website,
            ContactNumber = advisor_profile.ContactNumber,
            ID = advisor_profile.ID,
            ImageUrl = advisor_profile.ImageUrl,
            NameOfBusiness = advisor_profile.NameOfBusiness,
            StarRating = (int)advisor_profile.StarRating
        };
    }

    private async Task<AdvisorModel> FetchTipsAsync(AdvisorModel a)
    {
        var tips = await _tipService.GetTipsAsync(a.ID);

//CODE RUNS FINE TO HERE 

        var models = tips.Select(t => (TipModel)t);

        a.Tips = models.ToList();

//IF I LEAVE THIS BLOCK UNCOMMENTED 
//EXECUTION HALTS AT THE Task.WhenAll()

        return a;
    }
}

}
`

TipModel:

`
using Doshi.ApiCore.DTOs;
using Doshi.Core;
using System;

namespace Doshi.Core.Models
{
public class TipModel
{
public string Content { get; set; }
public string ID { get; set; }
public string OwnerID { get; set; }
public DateTime PublishDate { get; set; }
public bool Published { get; set; }
public string Title { get; set; }
public string Thumbnail { get; set; }

    public int Views { get; set; }

    public int Likes { get; set; }

    public int Dislikes { get; set; }

    public static explicit operator TipModel(TipDto tip)
    {
        var model = new TipModel
        {
            Content = tip.Content,
            ID = tip.ID,
            OwnerID = tip.OwnerID,
            PublishDate = tip.PublishDate,
            Published = tip.Published,
            Thumbnail = tip.Thumbnail,
            Title = tip.Title,
            Views = tip.Views
        };

        return model;
    }
}

}`

Any ideas what could be causing this.
Cheers!


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>