我有异步对象提取程序。
Fetcher
在初始化期间从要获取的对象的基本ID加载,并在完成后准备工作。此初始化应该是异步的。fetchMore(int count)
,这将开始加载异步对象。Fetcher
有两个信号initFinished
和fetchFinished` 我想在Fetcher
构造函数中开始初始化。在将信号连接到initFinished
之前,可以确定不会发出信号FetcherUser
吗?
class Fetcher : public QObject {
private:
QFutureWatcher<QList<ObjectId> > watcherInit;
QFutureWatcher<QList<Object> > watcherFetch;
QList<ObjectId> listIds;
QList<ObjectId> asyncInit() {
// Do some ids load
}
QList<Object> asyncFetchMore(QList<ObjectId> ids) {
// Do some objects load
}
signals:
void initFinished();
void fetchFinished(const QList<Object> &list);
public:
explicit Fetcher()
{
connect(&watcherInit, &QFutureWatcher<QList<ObjectId>>::finished, [this]{
// take ids and change init flag
emit initFinished();
});
auto future = QtConcurrent::run(&Fetcher::asyncInit, this);
watcherInit.setFuture(future)
}
void fetchMore(int count) {
// take some ids from list and QtConcurrnt run asyncFetchMore
}
};
//================================================
class FetcherUser : public QObject
{
Fetcher *fetcher = nullptr;
private:
void initFinished() {
//Start interaction with fetcher
}
public:
FetcherUser() {
fetcher = new Fetcher(); // Here async init started
connect(fetcher, &Fetcher::initFinished, this, &FetcherUser::initFinished); //and here connect signal
}
};
因此,我可以确定在构造函数完成并且启动了异步init之后,并且在信号连接到FetcherUser
之前,不会发出Fetcher
发出的信号吗?此外,如果我将在Fetcher
构造函数和信号连接之间进行一些操作,那么如何确保我没有丢失初始化信号?