Network call and thread safety in message / main thread

Hi!

I would like to implement an network call in order to do a GET in a api rest.
I’ve create a little class Network to do this.
I see that the NetworkDemo use Thread run method like this:

void run() override {
  auto result = getResultText (urlBox.getText());
  MessageManagerLock mml (this);
  if (mml.lockWasGained())
    resultsBox.loadContent (result);
}

However I’m not sure if resultsBox.loadContent (result); would be called in the Message thread :thinking:

In my class I do this:

void run() override {
  auto result = fetchData();
  MessageManagerLock mml (this);
  if (mml.lockWasGained()) {
    if(callback_) { callback_(result)
  }
}

void getData(std::function<void(juce::String)> completionHandler) { 
  callback_ = completionHandler;
  startThread();
}

But on the caller side, it seems to be called in different thread than the message thread:

// ...
network_.getData([this](juce::String result) {
  DBG(result); // Still in the Network thread and not the Message thread
});
// ..

The only way I found to be inside the Message thread is by using this code:

void run() override {
  auto result = fetchData();

  if(!threadShouldExit()) {
     // Is WeakReference necessary?
    auto weakRef = juce::WeakReference<VersionChecker>(this);

    juce::MessageManager::callAsync([weakRef, result] {
      if (weakRef == nullptr) { return; }
      if (weakRef->callback_) {
        weakRef->callback_(result);
      }
    });

  }
}

With this solution the caller get the completion called in the Message thread.

Which solution should I use?
Also does the duo WeakReference / callAsync is used correctly?

Thanks