Results 1 to 3 of 3

Thread: Need sodvice on this async code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Need sodvice on this async code

    Hi!

    Im currently writing a portable class library for scraping a website for information. It is a HR app, but sadly I have no API to get info so I have to resort to scraping html code.

    I have my basic models like Company, Employee etc.

    My problem is the employee, when selecting a company I get a list of employees which I load to a list in the viewmodel. I can scrape all info from this list (a html table) EXCEPT for the image url. That only appears when the user click the table row and a popup appears with some more info about the employee as well as a picture.

    I plan to solve this by using this pattern:

    Code:
     //var companies = await GetCompanies();
                myEmployees  = await GetEmployeesByCompany("SEKAR");
    
               
                Employee emp = (from em in myEmployees
                          where em.Id == myEmployees[10].Id
                                  select em).ToList().First();
                         
    
    
                 var x =   await AddImageToEmployee(emp);
    
                 emp = x;
    Note, this is for testing only. My plan is, when the user in the phone app taps on an employee in the list, I will query the viewmodel cache (myEmployees) for that particular employee, Call an async method to fetch the website for the popup, locate the url for the image, add that to the employee object and finally put the updated employee to the one in the list (emp = x).

    Does this approach has any problems? Should I change anything? I could have removed the emp = x and just used the code above, since emp is a reference type it would have been updated anyway, but I read somewhere you shouldn't write async methods that return void, so I return a Task<Employee> instead. Maybe ugly? How should I do this otherwise?

    Next the new page is loaded and the image is displayed as soon as the refreshed object return...

    please do suggest better ways to do this? It feels annoying having to go a second time to get info but since they have divided the info like that it is the only way... I tried to load the image when I loaded all employees but that took about 20 seconds for 50 employees so it is not an option.

    kind regads
    Henrik

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Need sodvice on this async code

    Quote Originally Posted by MrNorth View Post
    ...but I read somewhere you shouldn't write async methods that return void, so I return a Task<Employee> instead. Maybe ugly? How should I do this otherwise?
    They provided a non-generic Task class specifically for void return asynchronous methods so I wouldn't put too much stock into anyone that says you shouldn't use them. And I can't think of a reason why you shouldn't. You should use them if the situation calls for it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: Need some advice on this async code

    They provided a non-generic Task class specifically for void return asynchronous methods so I wouldn't put too much stock into anyone that says you shouldn't use them. And I can't think of a reason why you shouldn't. You should use them if the situation calls for it.
    Thanks for the hint. In .NET 4.5 there are so many "new" ways to do async calls that it gets confusing sometimes.

    With that said, what do you guys think about my "problem" in loading the data.

    My app is a relatively small one and I plan to have one viewmodel for my four pages (Login, ViewCompanies, ViewEmployeesByCompany, ViewEmployee)

    The problem comes to data loading since the data for an employee is split up between two different web pages and the performance hit of opening a connection to a page and load the data is for 50 employees about 20 seconds. It has to make 100 calls to the web server during that time, and I use HttpAgility to parse the web pages into an object tree I can query with LINQ.

    Is it a good enough idea to whenever the user select a company I load the employees for that company into an observablecollection and then display it in a longlistselector. That is pretty straight forward. The problem is when I want the picture of the employee, that in my model is a property of the employee, but the picture itself is located on a separate web site.

    What is the best way to handle this? I want to give the user a smooth experience. That is why I came up with this idea of just having a separate method in my viewmodel that when the user requests to display details about an employee I do this:

    * Query the observable collection of employees for an employee with that id the user clicked on

    * call the method "AddImageToEmployee(Employee e)" to add the url to the image (the obejct is a reference one so it should be updated in the collection as soon as I set the value with
    var task = AddImageToEmployee(emp);

    * set the property in viewmodel called "SelectedEmployee"

    * redirect user to employeeDetails page.

    Do please sugegst if this is good/bad practice?

    I have another "problem" later when I want to build the win 8 metro app. Then when they load the CustomersByCompany page, they want all the images to be displayed as thumbnails in the grid immediately when the user loads the employees. And I dont think anyone wants to wait 20 seconds for this. So I want to load the list first and display all names and then async I want the images to "pop up" when they are loaded. Like a 2 step operation.

    My solution is written in a PCL so I want a data loading solution that handles both scenarios.

    kind regards
    S

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width