Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Add contextObject to function subscribe #36

Open
luozikuan opened this issue Jan 7, 2021 · 1 comment
Open

Add contextObject to function subscribe #36

luozikuan opened this issue Jan 7, 2021 · 1 comment

Comments

@luozikuan
Copy link

luozikuan commented Jan 7, 2021

Considering this situation: Display a dialog, and then use AsyncFuture to send a network request, and the result will be displayed on the interface when the network request responds. I used the following logic (pseudo code):

Dialog::request()
{
    auto future = ... ; // request data from network
    observe(future).subscribe([=](QString data){  // onComplete
        ui->label->setText(data);
    },
    [=](){ // onCanceled
        ui->errorLabel->setText("error");
    });
}

But before the network reply, the user may close the dialog. In this case, the ui->label in onComplete has been destroyed, causing a crash.

Is there any plan to add contextObject to function subscribe? So I can modify code like this

Dialog::request()
{
    auto future = ... ; // request data from network
-    observe(future).subscribe([=](QString data){  // onComplete
+    observe(future).subscribe(this, [=](QString data){  // onComplete
        ui->label->setText(data);
    },
    [=](){ // onCanceled
        ui->errorLabel->setText("error");
    });
}
@luozikuan luozikuan changed the title Add contextObject to subscribe or add onCanceled to function context Add contextObject to function subscribe or add onCanceled to function context Jan 7, 2021
@luozikuan luozikuan changed the title Add contextObject to function subscribe or add onCanceled to function context Add contextObject to function subscribe Jan 7, 2021
@vpicaver
Copy link
Contributor

You can use observe(future).context(ui-label, {}, {} ) function instead of subscribe(). I've fixed a bunch of bugs on my fork.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants