2017-03-29 3 views
0

Afin de séparer mon interface graphique de la logique (qui récupère les données d'un service REST), j'ai refactorisé une logique dans un contrôleur.L'appel d'une méthode async ne fonctionne plus après le refactoring

Maintenant, seule une partie de la logique semble fonctionner.

Le composant GUI ressemble à ceci après refactoring (j'utilise le cadre de JUCE)

#pragma once 
#include "../../JuceLibraryCode/JuceHeader.h" 
#include "../../GUI.Controller/includes/ProjectEntryListController.h" 
#include "ProjectEntryListComponent.h" 
#include "LocalProjectEntryComponent.h" 

class ProjectBrowserTabComponent : public TabbedComponent 
{ 
public: 
    ProjectBrowserTabComponent(); 
    ~ProjectBrowserTabComponent(); 

private: 
    ProjectEntryListComponent m_remote_proj; 
    ProjectEntryListComponent m_local_proj; 
    ProjectEntryListController *pelccont = new ProjectEntryListController(&m_remote_proj); 
    ProjectEntryListController *pelccont2 = new ProjectEntryListController(&m_local_proj); 
}; 

Le contrôleur de l'interface graphique ressemble à ceci:

#define BOOST_THREAD_PROVIDES_FUTURE 
#include "../includes/ProjectEntryListController.h" 

template<typename R> 
bool isReady(std::future<R> const& f) 
{ 
    Logger::writeToLog("check future"); 
    return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready; 
} 

ProjectEntryListController::ProjectEntryListController(ProjectEntryListComponent *comp) { 
    m_comp = comp; 
    requestProjects(); 
} 

void ProjectEntryListController::requestProjects() 
{ 
    Logger::writeToLog("requesting projects"); 
    projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc); 
    Logger::writeToLog("requested projects"); 
} 

void ProjectEntryListController::backgroundCheckFuture() 
{ 
    timer = new boost::asio::deadline_timer(io_service, boost::posix_time::seconds(interval_secs)); 
    timer->async_wait(boost::bind(&ProjectEntryListController::fetchData, this, boost::asio::placeholders::error, timer)); 
    ioSvcFuture = std::async(std::launch::async, static_cast<size_t(boost::asio::io_service::*)()>(&boost::asio::io_service::run), &io_service); 
} 

void ProjectEntryListController::initData() { 
    requestProjects(); 
    backgroundCheckFuture(); 
} 

void ProjectEntryListController::fetchData(const boost::system::error_code& /*e*/, 
    boost::asio::deadline_timer* tmr) { 
    if (isReady(projectsFuture)) { 
     projects = projectsFuture.get(); 
     for (auto project : projects) 
     { 
      ProjectEntryComponent *pec = new ProjectEntryComponent(std::to_string(project.getId()), "222"); 
      m_comp->addListEntry(pec); 
      m_comp->repaint(); 
     } 
     Logger::writeToLog("got projs"); 
    } 
    else { 
     tmr->expires_at(tmr->expires_at() + boost::posix_time::seconds(interval_secs)); 
     tmr->async_wait(boost::bind(&ProjectEntryListController::fetchData, this, boost::asio::placeholders::error, tmr)); 
    } 
} 

les messages du journal de la méthode requestProjects apparaissent dans mon console, mais pas le message de journal de la méthode getProjects que j'appelle de manière asynchrone:

std::vector<Project> ProjectsController::getProjects() { 
    std::vector<Project> result; 
    if(serviceClient != nullptr) { 
     try 
     { 
      std::this_thread::sleep_for(std::chrono::seconds()); 
      std::cout << "controller requested projs\n"; 
      result = serviceClient->getAvailableProjects(); 
     } 
     catch (const std::exception&) 
     { 

     } 
    } 

    return result; 
} 

Cependant, lorsque je débogue dans le code, le débogueur (en utilisant VS 2015) est également capable d'accéder au message du journal.

Qu'est-ce que je fais mal?

Répondre

0

En fait, j'ai résolu cela maintenant.

1.) j'appelle la mauvaise méthode requestProjects au lieu de initData

2.) Je ne pouvais pas voir le résultat parce que la mise en œuvre de ProjectEntryComponent::ProjectEntryComponent(std::string name, std::string version) était manquante