Skip to content

Commit

Permalink
Removed protocol support due to self vs base object retention
Browse files Browse the repository at this point in the history
  • Loading branch information
MD AL MAMUN committed Oct 10, 2017
1 parent 9df2a98 commit bbb3662
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 109 deletions.
4 changes: 0 additions & 4 deletions Demo/Demo.xcodeproj/project.pbxproj
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
4782C2FD3F28267D0A736242 /* Pods_DemoTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 98ABDE1166974D890C0D32DA /* Pods_DemoTests.framework */; };
5E6ACB541F5863D30050E957 /* ProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E6ACB531F5863D30050E957 /* ProtocolTests.swift */; };
5E9E831F1BF79B3000C85B46 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9E831E1BF79B3000C85B46 /* AppDelegate.swift */; };
5E9E83211BF79B3000C85B46 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9E83201BF79B3000C85B46 /* ViewController.swift */; };
5E9E83241BF79B3000C85B46 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E9E83221BF79B3000C85B46 /* Main.storyboard */; };
Expand All @@ -32,7 +31,6 @@
1E9B7B66986C52EC97B65AAF /* Pods-Demo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Demo/Pods-Demo.debug.xcconfig"; sourceTree = "<group>"; };
371C581F5871A4EEFB5D8274 /* Pods-DemoTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-DemoTests/Pods-DemoTests.release.xcconfig"; sourceTree = "<group>"; };
579EF2C5ACCE845BC7962A04 /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
5E6ACB531F5863D30050E957 /* ProtocolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProtocolTests.swift; sourceTree = "<group>"; };
5E9E831B1BF79B3000C85B46 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
5E9E831E1BF79B3000C85B46 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
5E9E83201BF79B3000C85B46 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -106,7 +104,6 @@
children = (
5E9E83331BF79B3000C85B46 /* DemoTests.swift */,
5E9E83351BF79B3000C85B46 /* Info.plist */,
5E6ACB531F5863D30050E957 /* ProtocolTests.swift */,
);
path = DemoTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -355,7 +352,6 @@
buildActionMask = 2147483647;
files = (
5E9E83341BF79B3000C85B46 /* DemoTests.swift in Sources */,
5E6ACB541F5863D30050E957 /* ProtocolTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
45 changes: 0 additions & 45 deletions Demo/DemoTests/ProtocolTests.swift

This file was deleted.

53 changes: 0 additions & 53 deletions HasDisposeBag.swift

This file was deleted.

33 changes: 26 additions & 7 deletions NSObject+Rx.swift
Expand Up @@ -2,17 +2,36 @@ import Foundation
import RxSwift
import ObjectiveC

extension NSObject: HasDisposeBag {
fileprivate var disposeBagContext: UInt8 = 0

extension Reactive where Base: AnyObject {
func synchronizedBag<T>( _ action: () -> T) -> T {
objc_sync_enter(self.base)
let result = action()
objc_sync_exit(self.base)
return result
}
}

public extension Reactive where Base: HasDisposeBag {
public extension Reactive where Base: AnyObject {

/// a unique DisposeBag that is related to the Reactive.Base instance
var disposeBag: DisposeBag {
get { return base.disposeBag }
/// a unique DisposeBag that is related to the Reactive.Base instance only for Reference type
public var disposeBag: DisposeBag {
get {
return synchronizedBag {
if let disposeObject = objc_getAssociatedObject(base, &disposeBagContext) as? DisposeBag {
return disposeObject
}
let disposeObject = DisposeBag()
objc_setAssociatedObject(base, &disposeBagContext, disposeObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return disposeObject
}
}

set {
var mutableBase = base
mutableBase.disposeBag = newValue
synchronizedBag {
objc_setAssociatedObject(base, &disposeBagContext, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
}

0 comments on commit bbb3662

Please sign in to comment.