Skip to content

Q: How to sequentially resolve QDefer? #7

@digitalaspirin

Description

@digitalaspirin

I would appreciate your opinion on the following problem

The user retrieves from a backend a list of items and he is given the option to select a bunch of them and place a request to delete them. However, the backend interface only supports single delete requests. That is delete(id0) delete(id1) and so forth.

What is the best approach of crafting a method so that upon receiving a reply, resolve the QDefer object, and only then place a delete request for next pending item in the queue?

Currently I have approached it as follows:

connect(this,&MyList::deleteItem, this, &MyList::onDeleteItem, Qt::QueuedConnection);

… at some point the user clicks to delete selected items and deleteItem signal is fired…
m_itemsForDeletion = Qvector<int>{1,2,3,4,5};
emit delete();

QDeferred<Qstring> requestDelete(qint32 item)
{
	QDeferred<Qstring> retDeferred;
	… requestToDelete(item); //transaction with backend
	... wait for reply and resolve() or reject()
	retDeferred.resolve(“”);

	return retDeferred;
}

void MyList::onDeleteItem()
{
    if (m_itemsForDeletion.size()>0) {
        auto item = m_itemsForDeletion.takeFirst();
        requestDelete(item)
        .fail([](QString msg) {
             displayErrorMessage(msg);
         })
        .done([this](QString) {
             emit delete(); // keep emitting signal until all items have been deleted.
         });
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions