Git Product home page Git Product logo

Comments (5)

JensRavens avatar JensRavens commented on August 22, 2024

I would recommend you to use a similar style to disposables.

Interstellar uses a slightly different memory management concept: Instead of explicit disposables it's just plain ARC. As soon as there are no reference to an observable it will just be deallocated, taking all it's data with it. You can use this by observing some wrapper class around your data that cancels the request on deinit:

class Request {
  var task: URLSessionDataTask

  deinit {
    task.cancel()
  }
}

let request = ...
var observable: Observable<Request>? = Observable(request) // request is still running
obserable = nil // effectively canceling the request

from interstellar.

loudmouth avatar loudmouth commented on August 22, 2024

Thanks @JensRavens. I will revisit this next week.

Closing for now.

from interstellar.

loudmouth avatar loudmouth commented on August 22, 2024

Hey @JensRavens, I looked into it this morning and one thing I'd like to do is preserve the type signature of my Observable. In this case, the type is Observable<Result<DataType>> where DataType is the expected type after the network request returns successfully, and mapping from has occurred. This is in contrast to the above-suggested Observable<Request> type signature.

As an experiment, I checked out of a branch of Interstellar and simply added the following to Observable:

var cancellationBlock: ((Void) -> Void)?

deinit {
  cancellationBlock?()
}

And then of course I defined

observable.cancellationBlock = {
   task?.cancel()
}

This indeed enables the cancellation any underlying task by simply releasing the observable as suggested. I'm curious what you think of this approach and if a PR would be welcomed.

I would like to avoid creating a wrapper class around Observable as it would clutter the API provided by my library as consumers would either have to access Observable methods via myRequest.observable or this wrapper class would have to forward all methods of Observable.

from interstellar.

loudmouth avatar loudmouth commented on August 22, 2024

Hey @JensRavens , I now understand your suggestion a bit better after hacking for half a day and continuing to look at more resources about programming with Observables. I was happily surprised to find out that I didn't need to forward all methods when making a wrapper class to enable cancellation—rather that I could transform an Observable<Request> to an Observable<Result<ResultType>>, and then simply nullify my Observable<Result<ResultType>> instance to cancel the request.

Pasting the code here in case other's who are doing networking with Interstellar find this issue.

internal class Request {
    var task: URLSessionDataTask?

    init(task: URLSessionDataTask?) {
        self.task = task
    }

    deinit {
        print("cancelling request")
        task?.cancel()
    }
}

// Where the conversion of the datatask to the signal occurs.
func toObservable(url: URL, dataTaskToConvert: (URL, (Result<Data>) -> Void)) -> Observable<Result<Data>> {

    let dataObservable = Observable<Result<Data>>()

    let task: URLSessionDataTask? = dataTaskToConvert(url) { result in
        dataObservable.update(result)
    }
    let observable = Observable<Request>(Request(task: task))
    // Tranfrom from Observable<Request> to Observable<Data>
    return observable.flatMap { _ -> Observable<Result<Data>> in
        return responseObservable
    }
}

Thanks again.

from interstellar.

loudmouth avatar loudmouth commented on August 22, 2024

I may have spoken too soon. My implementation above immediate releases the Observable<Request> which contains the URLSessionTask of interest as soon as the function exits.

I'm feeling like either adding a cancellationBlock to Observable as suggested above, or some other implementation of Disposable is the way forward.

from interstellar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.