From ee447ac31e2ef1bc602f88d26be7f7571413da62 Mon Sep 17 00:00:00 2001 From: Hiroshi Kimura Date: Thu, 5 Oct 2023 16:51:46 +0900 Subject: [PATCH] Fix cache key for Derived (#423) --- Sources/VergeORM/Derived+ORM.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Sources/VergeORM/Derived+ORM.swift b/Sources/VergeORM/Derived+ORM.swift index e8f843eebb..7959a2f960 100644 --- a/Sources/VergeORM/Derived+ORM.swift +++ b/Sources/VergeORM/Derived+ORM.swift @@ -97,7 +97,7 @@ extension NonNullEntityWrapper: Equatable where Entity: Equatable {} extension NonNullEntityWrapper: Hashable where Entity: Hashable {} -private final class DerivedCacheKey: NSObject { +private final class DerivedCacheKey: NSObject, NSCopying { let entityType: ObjectIdentifier let entityID: AnyEntityIdentifier @@ -109,6 +109,18 @@ private final class DerivedCacheKey: NSObject { self.keyPathToDatabase = keyPathToDatabase } + func copy(with zone: NSZone? = nil) -> Any { + return DerivedCacheKey( + entityType: entityType, + entityID: entityID, + keyPathToDatabase: keyPathToDatabase + ) + } + + override var hash: Int { + entityID.hashValue ^ keyPathToDatabase.hashValue ^ entityType.hashValue + } + override func isEqual(_ object: Any?) -> Bool { guard let other = object as? DerivedCacheKey else { @@ -126,7 +138,7 @@ private final class DerivedCacheKey: NSObject { fileprivate final class _DerivedObjectCache { - private let storage = NSMapTable.strongToWeakObjects() + private let storage = NSMapTable.init(keyOptions: [.copyIn, .objectPersonality], valueOptions: [.weakMemory]) @inline(__always) private func key(entityID: E.EntityID, keyPathToDatabase: AnyKeyPath) -> DerivedCacheKey {