Skip to content

Commit

Permalink
Fix cache key for Derived (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 5, 2023
1 parent 55b98d9 commit ee447ac
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/VergeORM/Derived+ORM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -126,7 +138,7 @@ private final class DerivedCacheKey: NSObject {

fileprivate final class _DerivedObjectCache {

private let storage = NSMapTable<DerivedCacheKey, AnyObject>.strongToWeakObjects()
private let storage = NSMapTable<DerivedCacheKey, AnyObject>.init(keyOptions: [.copyIn, .objectPersonality], valueOptions: [.weakMemory])

@inline(__always)
private func key<E: EntityType>(entityID: E.EntityID, keyPathToDatabase: AnyKeyPath) -> DerivedCacheKey {
Expand Down

0 comments on commit ee447ac

Please sign in to comment.