Skip to content

Commit fb21fc6

Browse files
authored
Merge pull request #8 from zetasq/master
Fix reference cycle when using objc runtime.
2 parents 7b88c3a + 0ac8383 commit fb21fc6

File tree

7 files changed

+163
-4
lines changed

7 files changed

+163
-4
lines changed

TBEmptyDataSet.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
D33C7C781C44DF0A00E1687A /* EmptyDataView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C771C44DF0A00E1687A /* EmptyDataView.swift */; };
1212
D33C7C7A1C44E17000E1687A /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C791C44E17000E1687A /* Constants.swift */; };
1313
D33C7C7C1C44E21700E1687A /* ProtocolExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C7B1C44E21700E1687A /* ProtocolExtensions.swift */; };
14+
D4B0C98A1D17F6750040F428 /* WeakObjectContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4B0C9891D17F6750040F428 /* WeakObjectContainer.swift */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXFileReference section */
@@ -20,6 +21,7 @@
2021
D33C7C771C44DF0A00E1687A /* EmptyDataView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmptyDataView.swift; sourceTree = "<group>"; };
2122
D33C7C791C44E17000E1687A /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
2223
D33C7C7B1C44E21700E1687A /* ProtocolExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProtocolExtensions.swift; sourceTree = "<group>"; };
24+
D4B0C9891D17F6750040F428 /* WeakObjectContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeakObjectContainer.swift; sourceTree = "<group>"; };
2325
/* End PBXFileReference section */
2426

2527
/* Begin PBXFrameworksBuildPhase section */
@@ -57,6 +59,7 @@
5759
D33C7C771C44DF0A00E1687A /* EmptyDataView.swift */,
5860
D33C7C7B1C44E21700E1687A /* ProtocolExtensions.swift */,
5961
D33C7C731C44DCA000E1687A /* TBEmptyDataSet.swift */,
62+
D4B0C9891D17F6750040F428 /* WeakObjectContainer.swift */,
6063
);
6164
path = TBEmptyDataSet;
6265
sourceTree = "<group>";
@@ -148,6 +151,7 @@
148151
buildActionMask = 2147483647;
149152
files = (
150153
D33C7C781C44DF0A00E1687A /* EmptyDataView.swift in Sources */,
154+
D4B0C98A1D17F6750040F428 /* WeakObjectContainer.swift in Sources */,
151155
D33C7C7C1C44E21700E1687A /* ProtocolExtensions.swift in Sources */,
152156
D33C7C741C44DCA000E1687A /* TBEmptyDataSet.swift in Sources */,
153157
D33C7C7A1C44E17000E1687A /* Constants.swift in Sources */,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>TBEmptyDataSet.xcscheme_^#shared#^_</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>D33C7C661C44DBED00E1687A</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>
Binary file not shown.

TBEmptyDataSet/TBEmptyDataSet.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ extension UIScrollView: UIGestureRecognizerDelegate {
4040
// MARK: - Properties
4141
public var emptyDataSetDataSource: TBEmptyDataSetDataSource? {
4242
get {
43-
return objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource) as? TBEmptyDataSetDataSource
43+
let container = objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource) as? WeakObjectContainer
44+
return container?.object as? TBEmptyDataSetDataSource
4445
}
4546
set {
4647
if let newValue = newValue {
47-
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
48+
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
4849

4950
if self is UITableView {
5051
UITableView.tb_swizzleTableViewReloadData()
@@ -61,11 +62,12 @@ extension UIScrollView: UIGestureRecognizerDelegate {
6162

6263
public var emptyDataSetDelegate: TBEmptyDataSetDelegate? {
6364
get {
64-
return objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate) as? TBEmptyDataSetDelegate
65+
let container = objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate) as? WeakObjectContainer
66+
return container?.object as? TBEmptyDataSetDelegate
6567
}
6668
set {
6769
if let newValue = newValue {
68-
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
70+
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
6971
} else {
7072
handlingInvalidEmptyDataSet()
7173
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// WeakObjectContainer.swift
3+
// TBEmptyDataSet
4+
//
5+
// Created by Zhu Shengqi on 6/20/16.
6+
// Copyright © 2016 Teambition. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class WeakObjectContainer: NSObject {
12+
weak var object: AnyObject?
13+
14+
init(object: AnyObject) {
15+
self.object = object
16+
super.init()
17+
}
18+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0730"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "D3565A9E1BFD9449002CFA16"
18+
BuildableName = "TBEmptyDataSetExample.app"
19+
BlueprintName = "TBEmptyDataSetExample"
20+
ReferencedContainer = "container:TBEmptyDataSetExample.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
</Testables>
32+
<MacroExpansion>
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "D3565A9E1BFD9449002CFA16"
36+
BuildableName = "TBEmptyDataSetExample.app"
37+
BlueprintName = "TBEmptyDataSetExample"
38+
ReferencedContainer = "container:TBEmptyDataSetExample.xcodeproj">
39+
</BuildableReference>
40+
</MacroExpansion>
41+
<AdditionalOptions>
42+
</AdditionalOptions>
43+
</TestAction>
44+
<LaunchAction
45+
buildConfiguration = "Debug"
46+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
47+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
48+
launchStyle = "0"
49+
useCustomWorkingDirectory = "NO"
50+
ignoresPersistentStateOnLaunch = "NO"
51+
debugDocumentVersioning = "YES"
52+
debugServiceExtension = "internal"
53+
allowLocationSimulation = "YES">
54+
<BuildableProductRunnable
55+
runnableDebuggingMode = "0">
56+
<BuildableReference
57+
BuildableIdentifier = "primary"
58+
BlueprintIdentifier = "D3565A9E1BFD9449002CFA16"
59+
BuildableName = "TBEmptyDataSetExample.app"
60+
BlueprintName = "TBEmptyDataSetExample"
61+
ReferencedContainer = "container:TBEmptyDataSetExample.xcodeproj">
62+
</BuildableReference>
63+
</BuildableProductRunnable>
64+
<AdditionalOptions>
65+
</AdditionalOptions>
66+
</LaunchAction>
67+
<ProfileAction
68+
buildConfiguration = "Release"
69+
shouldUseLaunchSchemeArgsEnv = "YES"
70+
savedToolIdentifier = ""
71+
useCustomWorkingDirectory = "NO"
72+
debugDocumentVersioning = "YES">
73+
<BuildableProductRunnable
74+
runnableDebuggingMode = "0">
75+
<BuildableReference
76+
BuildableIdentifier = "primary"
77+
BlueprintIdentifier = "D3565A9E1BFD9449002CFA16"
78+
BuildableName = "TBEmptyDataSetExample.app"
79+
BlueprintName = "TBEmptyDataSetExample"
80+
ReferencedContainer = "container:TBEmptyDataSetExample.xcodeproj">
81+
</BuildableReference>
82+
</BuildableProductRunnable>
83+
</ProfileAction>
84+
<AnalyzeAction
85+
buildConfiguration = "Debug">
86+
</AnalyzeAction>
87+
<ArchiveAction
88+
buildConfiguration = "Release"
89+
revealArchiveInOrganizer = "YES">
90+
</ArchiveAction>
91+
</Scheme>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>TBEmptyDataSetExample.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>1</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>D3565A9E1BFD9449002CFA16</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)