This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@UIApplicationMain | |
final class AppDelegate: UIResponder, UIApplicationDelegate { | |
func applicationDidReceiveMemoryWarning(_ application: UIApplication) { | |
var properties: [String: String] = [:] | |
if let footprint = memoryFootprint { | |
properties["memory"] = String(format: "%d MB", footprint / 1024 / 1024) | |
} | |
// Trigger "Analytics Event" or "Non-Fatal Crash" or persist locally and send to server later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
deinit { | |
print("View Controller has been released") // Set breakpoint on this line | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public class MemoryChecker { | |
public static func verifyDealloc(object: AnyObject?) { | |
#if DEBUG | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { [weak object] in | |
if let object = object { | |
fatalError("Class Not Deallocated: \(String(describing: object.classForCoder ?? object)))") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class NavigationController: UINavigationController { | |
override func popViewController(animated: Bool) -> UIViewController? { | |
let viewController = super.popViewController(animated: animated) | |
MemoryChecker.verifyDealloc(object: viewController) | |
return viewController | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class NavigationController: UINavigationController { | |
deinit { | |
viewControllers.forEach { MemoryChecker.verifyDealloc(object: $0) } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class TableView: UITableView { | |
private var allCells: [WeakBox<UITableViewCell>] = [] | |
private func appendIfNotPresent(_ cell: UITableViewCell) { | |
if allCells.first(where: { $0.unbox === cell }) == nil { | |
allCells.append(WeakBox(cell)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CollectionView: UICollectionView { | |
private var allCells: [WeakBox<UICollectionViewCell>] = [] | |
private func appendIfNotPresent(_ cell: UICollectionViewCell) { | |
if allCells.first(where: { $0.unbox === cell }) == nil { | |
allCells.append(WeakBox(cell)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class WeakBox<A: AnyObject> { | |
weak var unbox: A? | |
init(_ value: A) { | |
unbox = value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentViewController: UIViewController, ChildViewControllerProtocol { | |
let childViewController = ChildViewController() | |
func prepareChildViewController() { | |
childViewController.delegate = self // ❌ Strong reference, retain cycle | |
} | |
} | |
protocol ChildViewControllerProtocol: class {} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
weak var delegate: ChildViewControllerProtocol? // ✅ No retain cycle |
OlderNewer