2017-03-29 1 views
0

내 GUI를 로직 (REST 서비스에서 데이터를 가져 오는)에서 분리하기 위해 일부 로직을 컨트롤러로 리팩토링했다.리팩토링 후 비동기 메서드 호출이 더 이상 작동하지 않는다

이제 로직의 일부만 작동하는 것 같습니다. GUI 컴포넌트가

#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); 
}; 

그래픽 사용자 인터페이스 컨트롤러는 다음과 같다 (필자는 JUCE 프레임 워크를 사용하고 있습니다) 리팩토링 후 다음과 같습니다

:

#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)); 
    } 
} 

requestProjects 방법의 로그 메시지

가에 등장하는 내 콘솔이지만 비동기식으로 호출하는 getProjects 메서드의 로그 메시지는 아닙니다.

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; 
} 

그러나 코드로 디버깅 할 때 디버거 (VS 2015 사용)도 로그 메시지로 이동할 수 있습니다.

내가 뭘 잘못하고 있니?

답변

0

사실 지금이 문제를 해결했습니다.

1) 나는 ProjectEntryComponent::ProjectEntryComponent(std::string name, std::string version)의 구현

누락 되었기 때문에 나는 결과를 볼 수 없었다 requestProjects 대신) initData

2. 잘못된 메소드를 호출했다

관련 문제