ReactKit / SwiftTask

Promise + progress + pause + cancel + retry for Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create tasks manager to retain tasks

Koshub opened this issue · comments

Is it possible to retain task while it is executed in one scope? I have found that tasks deinit after creating )) even they are returned from success block of other task. Probably we need something that will retain task while it is executing like in next example:

public class Task<Progress, Value, Error> {

    //...

    class func createTask<Progress, Value, Error>(initClosure: InitClosure) -> Task<Progesss, Value, Error> {
        // Paused
        let task = Task<Progesss, Value, Error>(paused: true, initClosure: initClosure)
        task.onFinish = {
            TasksRegistry.unregister(task) // Release
        }
        TasksRegistry.register(task) // Retain
        return task
    }

    //...
}

Or is there any other ways to do so?

Btw I use default value for weakified paramenter (false)

When Task starts running, it is normally retained by its internal async operation e.g. dispatch queue, so there's no need for adding TasksRegistry.

I think your code example is a non-started (paused) task, and that will exactly lead to deinit if not started. But adding to TasksRegistry will beome a potential memory leak issue in this case, as it might never start running. I would say it is user's job to manage non-started task until its run, adding TasksRegistry somewhere not in global scope.

Example with TasksRegistry is pseudocode. Sorry, it was my mistake. Now it works correctly and predictable.