As the documentation says
the subscribe function returns a one-time subscription, which can be used to cancel calculations and free resources.
The preferred way to end these free calls is to use .addDisposableTo (disposeBag) or in some equivalent way. When the disposeBag released, the subscription will be automatically located.
Actually your example looks fine in terms of rules, but it's pretty bad;) (It would also be nice if you just returned this disposable ):
public static func changes() -> Observable<ChangeSet>{ return Observable.create{ observable in // return Observable<NSData> let request = HTTPService.get("https://httpbin.org/get") return request.subscribe( onNext: { data in // Do more work to transform this data // into something meaningful for the application. // For example purposes just use an empty object observable.onNext(ChangeSet()) observable.onCompleted() }, onError:{ error in observable.onError(error) }) }
But how do you return Observeble , I wonder why you just do not use the map operator?
In your example, it would be something like this:
public static func changes() -> Observable<ChangeSet> { return HTTPService.get("https://httpbin.org/get") .map(ChangeSet.init) }
source share