-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce CacheClearer to force-clear static caches
- Loading branch information
1 parent
89f75ba
commit 66241fa
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
|
||
@_spi(CacheManagement) | ||
public struct CacheClearer { | ||
|
||
/// Clears all static caches that are in use. | ||
/// | ||
/// Blueprint leverages static caching to improve performance however there are situations in which | ||
/// this can cause object lifetimes to be extended unexpectedly, especially in cases where cached | ||
/// views reference other objects. | ||
/// | ||
/// - WARNING: Clearing these caches can have global performance implications. This method | ||
/// should be invoked sparingley and only after other workarounds to manage object lifetimes have failed. | ||
@_spi(CacheManagement) | ||
public static func clearStaticCaches() { | ||
UIViewElementMeasurer.shared.removeAllObjects() | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import XCTest | ||
@testable @_spi(CacheManagement) import BlueprintUI | ||
|
||
class CacheClearerTests: XCTestCase { | ||
|
||
func test_clearStaticCaches() { | ||
|
||
struct TestElement: UIViewElement { | ||
func makeUIView() -> UIView { | ||
UIView() | ||
} | ||
|
||
func updateUIView(_ view: UIView, with context: BlueprintUI.UIViewElementContext) {} | ||
} | ||
|
||
let _ = UIViewElementMeasurer.shared.measure( | ||
element: TestElement(), | ||
constraint: .unconstrained, | ||
environment: .empty | ||
) | ||
|
||
XCTAssertEqual(UIViewElementMeasurer.shared.cachedViewCount, 1) | ||
|
||
CacheClearer.clearStaticCaches() | ||
|
||
XCTAssertEqual(UIViewElementMeasurer.shared.cachedViewCount, 0) | ||
} | ||
} | ||
|