Skip to content

Commit

Permalink
DisposedBag
Browse files Browse the repository at this point in the history
  • Loading branch information
younari committed Dec 2, 2017
1 parent e1d8fa5 commit d28ba5c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
4 changes: 4 additions & 0 deletions RxTest/RxTest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
D7A475351FD05D7000CFE8A8 /* UIImagePickerController+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A475341FD05D7000CFE8A8 /* UIImagePickerController+Rx.swift */; };
DA2015CEB4A1BFFB13088FC2 /* Pods_RxTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D5A01258F47BA2F793AFFBA /* Pods_RxTest.framework */; };
DDA681431FD295E2000F0A3D /* UIAlertRx.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA681421FD295E2000F0A3D /* UIAlertRx.swift */; };
DDA681451FD2A201000F0A3D /* DisposeBagController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA681441FD2A201000F0A3D /* DisposeBagController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -59,6 +60,7 @@
D7A475311FD05A2900CFE8A8 /* ImagePickerTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePickerTestViewController.swift; sourceTree = "<group>"; };
D7A475341FD05D7000CFE8A8 /* UIImagePickerController+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImagePickerController+Rx.swift"; sourceTree = "<group>"; };
DDA681421FD295E2000F0A3D /* UIAlertRx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIAlertRx.swift; sourceTree = "<group>"; };
DDA681441FD2A201000F0A3D /* DisposeBagController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisposeBagController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -131,6 +133,7 @@
D7A475311FD05A2900CFE8A8 /* ImagePickerTestViewController.swift */,
DDA681421FD295E2000F0A3D /* UIAlertRx.swift */,
D76953B41FD1A68200C7A006 /* GroupListViewController.swift */,
DDA681441FD2A201000F0A3D /* DisposeBagController.swift */,
);
path = RxTest;
sourceTree = "<group>";
Expand Down Expand Up @@ -347,6 +350,7 @@
D7A475351FD05D7000CFE8A8 /* UIImagePickerController+Rx.swift in Sources */,
D76953B51FD1A68200C7A006 /* GroupListViewController.swift in Sources */,
DDA681431FD295E2000F0A3D /* UIAlertRx.swift in Sources */,
DDA681451FD2A201000F0A3D /* DisposeBagController.swift in Sources */,
D7A475321FD05A2900CFE8A8 /* ImagePickerTestViewController.swift in Sources */,
D7697A161FCC6C34007F5B5D /* AnimationViewController.swift in Sources */,
D7A4752A1FCF144100CFE8A8 /* URLToImageViewController.swift in Sources */,
Expand Down
57 changes: 57 additions & 0 deletions RxTest/RxTest/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@
<segue destination="Hau-Yy-c47" kind="show" id="KjR-u1-h5h"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="EdS-TO-fgw" style="IBUITableViewCellStyleDefault" id="5rK-0T-TIj">
<rect key="frame" x="0.0" y="220" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="5rK-0T-TIj" id="E5U-ua-lqF">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Dispose Bag" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="EdS-TO-fgw">
<rect key="frame" x="16" y="0.0" width="343" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<segue destination="JwG-6U-EiY" kind="show" id="JBp-jO-poe"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
Expand Down Expand Up @@ -361,6 +381,43 @@
</objects>
<point key="canvasLocation" x="-2174" y="763"/>
</scene>
<!--Dispose Bag Controller-->
<scene sceneID="MbV-a7-yjy">
<objects>
<viewController id="JwG-6U-EiY" customClass="DisposeBagController" customModule="RxTest" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="ure-1v-pvK">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wbg-IY-aqx">
<rect key="frame" x="16" y="115" width="148" height="35"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" name="alternateSelectedControlColor" catalog="System" colorSpace="catalog"/>
<state key="normal" title="DisposeBag">
<color key="titleColor" name="alternateSelectedControlTextColor" catalog="System" colorSpace="catalog"/>
<color key="titleShadowColor" name="alternateSelectedControlColor" catalog="System" colorSpace="catalog"/>
</state>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="mmg-xR-XA8">
<rect key="frame" x="16" y="158" width="343" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<viewLayoutGuide key="safeArea" id="zyg-WA-Jgz"/>
</view>
<connections>
<outlet property="disposeBagBtn" destination="wbg-IY-aqx" id="Wg2-Kf-AAk"/>
<outlet property="progressLabel" destination="mmg-xR-XA8" id="wqm-ln-pAd"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Kpf-Gz-bp6" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-18.399999999999999" y="-463.71814092953525"/>
</scene>
<!--Ninenine Table View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
Expand Down
47 changes: 47 additions & 0 deletions RxTest/RxTest/DisposeBagController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// DisposeBagController.swift
// RxTest
//
// Created by 김기윤 on 02/12/2017.
// Copyright © 2017 leonard. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class DisposeBagController: UIViewController {

@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var disposeBagBtn: UIButton!

var disposeBag: DisposeBag = DisposeBag()
var testDisposeBag: DisposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
bind()
}
}

extension DisposeBagController {

func bind() {
Observable<String>.create { (observer) -> Disposable in
observer.onNext("HELLOWORLD")
return Disposables.create {
print("Disposed in Create")
}
}.subscribe(
onNext: { text in
self.progressLabel.text = text
}, onDisposed: {
print("Disposed in subscribe")
}).disposed(by: testDisposeBag)

disposeBagBtn.rx.tap.asObservable().subscribe(
onNext: { [weak self] _ in
self?.testDisposeBag = DisposeBag()
}).disposed(by: disposeBag)
}

}
5 changes: 2 additions & 3 deletions RxTest/RxTest/ImagePickerTestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension ImagePickerTestViewController {


// observeOn & subscribeOn 을 통한 thread 관리
uploadButton.rx.tap.asObservable()
uploadButton.rx.tap.asObservable().subscribeOn(MainScheduler.instance)
.flatMap{ [weak self] _ -> Observable<Void> in
UIAlertController.rx.showAlertTo(self, title: "업로드", message: "업로드 하시겠습니까?")
}.map{ [weak self] _ -> UIImage? in
Expand All @@ -57,8 +57,7 @@ extension ImagePickerTestViewController {
.flatMap { (image: UIImage?) -> Observable<Float> in
guard let img = image else { return Observable.empty() }
return API.upload(image: img)
}.subscribeOn(MainScheduler.instance)
.bind(to: progressbar.rx.progress)
}.bind(to: progressbar.rx.progress)
.disposed(by: disposeBag)
}

Expand Down

0 comments on commit d28ba5c

Please sign in to comment.