Comments (5)
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.
Thanks @JensRavens. I will revisit this next week.
Closing for now.
from interstellar.
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.
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.
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)
- Signal could be struct HOT 2
- Adding Signal update(f: T? -> T) HOT 5
- Interstellar 2.0 HOT 18
- Debounce using GCD? HOT 1
- Yosemite support HOT 5
- Memory leak when map'ing signals HOT 4
- More complex unsubscription in v2 HOT 5
- Possible data race in function `subscribe`
- Not sure about how `debounce` works HOT 2
- Swift 3 version HOT 2
- Missing comma in .podspec file
- Question regarding Thread class HOT 2
- global(qos:) only available on OSX 10.10 HOT 1
- Debounce doesn't deliver last call to update
- Using Queue with observable, maybe race condition? HOT 1
- Filter HOT 3
- Updating observable's value without triggering update HOT 1
- Zero-Knowledge Disposal HOT 5
- Swift's 5 Result type HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interstellar.