c++gtk3gio

Why does Gio::Settings require a delay?


I'm writing an application in C++ that uses both Qt and GIO. It happens to be an embedded Linux platform, but I don't know if that matters much. I have a function that sets a setting that another program uses:

void setCityName(const QString &cityName) 
{
    const Glib::RefPtr<Gio::Settings> settings = Gio::Settings::create("org.example.city");
    settings->set_string("city-name", cityName.toUtf8().data()); 
}

This is done at the very end of my program appears to work, but only if I add a delay:

void finish(QString* cityname) {
    qDebug() << "Cityname: " << *cityname;
    setCityName(*cityname);
    // TODO: this is an ugly hack.  Without it, the settings 
    // do not seem to take effect, but I can find no reason
    // for this.
    std::this_thread::sleep_for(200ms);
    emit done();
}

I have instrumented the code and verified that cityname which belongs to another object, is not destroyed until after this code runs, either with or without the delay. Without the delay, everything appears to run normally, but the settings are not actually changed.

Can someone explain why the delay is necessary, and how I can replace it with something more elegant?


Solution

  • As the documentation says:

    Writes made to a GSettings are handled asynchronously.

    Call Gio::Settings.sync: https://docs.gtk.org/gio/type_func.Settings.sync.html