close
Skip to content

disposeBag: add a HasDisposeBag protocol#41

Merged
ashfurrow merged 1 commit into
RxSwiftCommunity:masterfrom
twittemb:feature/disposeBagable
Aug 31, 2017
Merged

disposeBag: add a HasDisposeBag protocol#41
ashfurrow merged 1 commit into
RxSwiftCommunity:masterfrom
twittemb:feature/disposeBagable

Conversation

@twittemb
Copy link
Copy Markdown

In this commit, we try to conform to protocol oriented programming. It is more flexible to
introduce a DisposeBagable protocol so that every object that respect this one
can have a DisposeBag (NSObject does respect that protocol now)

Copy link
Copy Markdown
Member

@ashfurrow ashfurrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Anyone from @RxSwiftCommunity/contributors available to review?

Comment thread NSObject_Rx/DisposeBagable.swift Outdated
get {
var disposeBag: DisposeBag!
doLocked {
let lookup = objc_getAssociatedObject(self, &AssociatedKeys.rxDisposeBag) as? DisposeBag
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting – I didn't expect associated objects to work with non-NSObject subclasses. You've verified this works?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a Unit test for this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, there is a new Test for that. It works.

Comment thread Readme.md Outdated

It'll work just like a property: when the instance is deinit'd, the `DisposeBag` gets disposed. It's also a read/write property, so you can use your own, too.

If you want to add a DisposeBag to an Object that does not inherit from NSObject, you can also implement the protocol "DisposeBagable", and you're good to go. This protocol provides a default DisposeBag called rxDisposeBag.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent addition to the readme 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering on why we should call this rxDisposebag given the fact it's from RxSwift is rather implicit? what about just calling it disposeBag across the board?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will fix that.

let address2 = Unmanaged<AnyObject>.passUnretained(newDisposeBag as AnyObject).toOpaque()
XCTAssert(address1 == address2)
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good test coverage, awesome!

@freak4pc
Copy link
Copy Markdown
Member

My only gotcha with this is I really don't think this is an intuitive protocol name. Definitely an opinionated thing as naming is hard :)

Great implementation though!

@twittemb
Copy link
Copy Markdown
Author

For the name, there is another possibility: HasDisposeBag
Is this better ?

@freak4pc
Copy link
Copy Markdown
Member

freak4pc commented Aug 28, 2017

What about Disposing (also possible Disposer but less common than using ing when able doesn't make english sense) ?

It's very tricky since naming conventions are hard in this sense :)

Opinions ? @RxSwiftCommunity/contributors

@freak4pc
Copy link
Copy Markdown
Member

You should also fix CI :)

@twittemb
Copy link
Copy Markdown
Author

I like the HasDisposeBag, because it is very explicit when you read the signature of the class that will implements it.

@twittemb
Copy link
Copy Markdown
Author

CI is broken for the Demo app. It was already broken before I think.
As far I as I can tell, the demo app is just for Unit Testing. I think it could be removed since we can implement Unit Tests directly in the main project. Am I wrong ?

@freak4pc
Copy link
Copy Markdown
Member

I'm definitely OK with this @twittemb .
Master is actually passing green so this seems to be related to the changes you made here

@ashfurrow
Copy link
Copy Markdown
Member

ashfurrow commented Aug 28, 2017

Yeah, CI is broken for this PR but it's not your fault! Looks like you're a Carthage user right? We just need to run pod update NSObject-Rx inside the Demo directory. I can take care of that if this PR is otherwise good to go.

EDIT: I've updated the readme to have the CI badge display the latest master build, instead of just the latest build of any branch.

@freak4pc
Copy link
Copy Markdown
Member

I think the two pending items are :

  1. Having the associated var named disposeBag across the board instead of rxDisposebag and updating docs accordingly
  2. Gather feedback from community regarding protocol name. Or if there is no feedback for a day or two just leave the current naming which is fine as well :)

@twittemb
Copy link
Copy Markdown
Author

Ok. I've made the following changes:

  • the var is now named "disposeBag",
  • the protocols is now named "HasDisposeBag", it should be a more consensual choice.

@freak4pc
Copy link
Copy Markdown
Member

freak4pc commented Aug 28, 2017

Thanks for the disposeBag change. 💯

Regarding the Has prefix, I've never seen a naming convention where that is used for a protocol so I still think it would be best to wait for some feedback from other contributors. And if not we can perhaps just leave DisposeBagable.

HasDisposeBag feels a bit off perhaps? I guess i'm not religious about it but just not sure that's proper naming

@ashfurrow
Copy link
Copy Markdown
Member

See this is why I liked having all protocols suffixed with "Type", but that went away in Swift 3 :\ I'm in favour of either – we can always change it later + add a typealias for the old name.

@twittemb
Copy link
Copy Markdown
Author

twittemb commented Aug 28, 2017

I was inspired by the following article by Krzysztof Zabłocki, who is involved in the swift community.
http://merowing.info/2017/04/using-protocol-compositon-for-dependency-injection/

He uses the "Has..." to specify every kinds of Services a class can provide (in a DI context)

Another idea: It could be something like "DisposeBagProvider" ... but it is pretty long.

@freak4pc
Copy link
Copy Markdown
Member

freak4pc commented Aug 28, 2017

What about DisposeBagProvider (e.g. it provides a dispose bag)

also @ashfurrow RxSwift itself still uses ObservableConvertibleType etc so I think that's fine.
Problem is "DisposeBagType" means the object itself is a dispose bag, and not that it is able to dispose thing.

Interesting about Krzysztof's article. We can keep it this way for now. I'm still a bit iffy about this :) But that's personal taste.

I'd prefer Disposing (e.g. it can Dispose) or the aforementioned DisposeBagProvider. But let's leave it at HasDisposeBag and be done with it I guess !

@twittemb
Copy link
Copy Markdown
Author

twittemb commented Aug 28, 2017

ok. Thanks for your feedback anyway.
Just for the record, NSObject-Rx was useful for the project I am doing on my Github. It is about implementing Navigation in iOS apps using a Weaving Pattern. I have experienced many architecture solutions and the best I've found so far was MVVM (with RxSwift) + Coordinator pattern. But I found the Coordinator pattern a bit verbose and there was a lot of boilerplate code to write each time (many delegate patterns). So I combined Coordinator + RxSwift + the idea of navigation state (a bit like Redux) to make a Framework that could ease the implementation of such a pattern. The idea is really to:

  • remove navigation code from ViewController for a best separation of concerns,
  • promote dependency injection
  • allow to cut the storyboards into atomic pieces to ease the reusability (in different navigation contexts) and the collaboration (no more storyboards conflicts).
    There is still a lot to do but I have something working. Here is the repo:
    https://github.com/twittemb/Weavy

Feel free to contribute :-)

@twittemb
Copy link
Copy Markdown
Author

Hi,
What is the validation process for this PR ? Is there a delay to allow the community to review it ?
thanks.

@freak4pc
Copy link
Copy Markdown
Member

I think there's agreement with me and @ashfurrow so that's enough.

Let's just get the CI working and LGTM!

@ashfurrow
Copy link
Copy Markdown
Member

Yeah, we should definitely get this merged. Any ideas what's wrong with the tests on Travis? I can help take a look tomorrow, let me know.

@freak4pc
Copy link
Copy Markdown
Member

I'm on an earlier time zone, I'll take a look around tomorrow my morning :)

Comment thread Demo/Podfile.lock Outdated
EXTERNAL SOURCES:
NSObject+Rx:
:path: "../"
:path: ../
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be ../NSObject_Rx to make CI happy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this points to the podspec so it should be fine

@freak4pc
Copy link
Copy Markdown
Member

Hey @twittemb
Can you remove the Podfile.lock and Cartfile.resolved changes from this commit? I don't think it belongs as part of this change and is probably what causing the CI to fail.

Once that is changed and merged @ashfurrow can separately release a new version

@twittemb
Copy link
Copy Markdown
Author

twittemb commented Aug 31, 2017

Hey @freak4pc
I have made the changes.
IMHO the Cartfile.resolved should be in the repo. This is what allows to do a "carthage bootstrap" instead of a "carthage update" to ensure the consistency of the dependencies versions.

@freak4pc
Copy link
Copy Markdown
Member

freak4pc commented Aug 31, 2017

@twittemb I didn't mean to remove the entire Podfile.lock ... I mean you should not have changed it... Can you fix that ? Right now you just deleted the entire file. Regarding Carthage.resolved, might be but that shouldn't be part of this specific PR IMO

@twittemb
Copy link
Copy Markdown
Author

@freak4pc ok sorry for the misunderstanding ... I've restored the Podfile.lock with the changes you recommend.

@freak4pc
Copy link
Copy Markdown
Member

This PR still introduces Podfile.lock changes...
My point is this PR shouldn't have any changes on Podfile.lock or Cartfile.resolved at the moment.

e392605a-3c39-4066-a038-abe6b7ef54bb

In this commit, we try to conform to protocol oriented programming. It is more flexible to
introduce a HasDisposeBag protocol so that every object that respect this one
can have a DisposeBag (NSObject does respect that protocol now)
@twittemb
Copy link
Copy Markdown
Author

@freak4pc should be alright now :-)

@freak4pc
Copy link
Copy Markdown
Member

@twittemb Yup! looks good! Let's see if CI clears and I'm good to merge this.
Thanks for your patience and cooperation !

@freak4pc freak4pc changed the title disposeBag: add a DisposeBagable protocol disposeBag: add a HasDisposeBag protocol Aug 31, 2017
@freak4pc
Copy link
Copy Markdown
Member

Very confusing 🤔
Master is passing and there's only code changes, so not sure why the module isn't passing.

@ashfurrow any thoughts here? I didn't setup the CI here so not sure if any assumptions I'm missing

@ashfurrow
Copy link
Copy Markdown
Member

It's possible that Travis has changed something on their end. I'll investigate and get back to everyone.

@twittemb
Copy link
Copy Markdown
Author

ok thanks @ashfurrow
If this can help, I can still make a first PR that would remove Demo app and integrate the tests that were done in it directly into the main NSObject-Rx project. I could rebase the current PR on this new one.

@freak4pc
Copy link
Copy Markdown
Member

@twittemb If you have the time that would be great! Might also resolve our issue here altogether.

@twittemb
Copy link
Copy Markdown
Author

@freak4pc @ashfurrow I'll try to do it today (Montreal time zone)

@ashfurrow
Copy link
Copy Markdown
Member

I can reproduce the problem locally – will have a fix shortly. We might as well keep this PR then.

@ashfurrow ashfurrow mentioned this pull request Aug 31, 2017
@ashfurrow
Copy link
Copy Markdown
Member

Okay, so this looks like an instance of a pretty common issue – Carthage and CocoaPods not being in sync. Happens all the time on Moya :) I've opened #42 with the changes from this PR as well as the changes needed to sort things out.

@ashfurrow ashfurrow merged commit 99868e3 into RxSwiftCommunity:master Aug 31, 2017
ashfurrow added a commit that referenced this pull request Aug 31, 2017
@ashfurrow
Copy link
Copy Markdown
Member

Thanks a lot for contributing! I've invited you to join the RxSwiftCommunity GitHub organization – no pressure to accept! If you'd like more information on what that means, check out our contributor guidelines and feel free to reach out with any questions.

@twittemb twittemb deleted the feature/disposeBagable branch August 31, 2017 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants