Skip to content

Commit

Permalink
Minor re-write to avoid concurrency issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
radianttap committed Mar 23, 2024
1 parent 075ad96 commit 6f632da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
// Copyright © 2023 ZeeZide GmbH.
//

private var _propertyIsUniqueAssociatedKey: UInt8 = 42

extension NSPropertyDescription {

private struct AssociatedKeys {
nonisolated(unsafe) static var propertyIsUniqueAssociatedKey: Void? = nil
}

public internal(set) var isUnique: Bool {
// Note: isUnique is only used during schema construction!
set {
if newValue {
objc_setAssociatedObject(self, &_propertyIsUniqueAssociatedKey,
objc_setAssociatedObject(self, &AssociatedKeys.propertyIsUniqueAssociatedKey,
type(of: self), .OBJC_ASSOCIATION_ASSIGN)
}
else {
objc_setAssociatedObject(self, &_propertyIsUniqueAssociatedKey, nil, .OBJC_ASSOCIATION_RETAIN)
objc_setAssociatedObject(self, &AssociatedKeys.propertyIsUniqueAssociatedKey, nil, .OBJC_ASSOCIATION_RETAIN)
}
#if false // do we need this? The entity might not yet be setup?
#if false // do we need this? The entity might not yet be setup?
guard !entity.isPropertyUnique(self) else { return }
entity.uniquenessConstraints.append( [ self ])
#endif
#endif
}
get {
objc_getAssociatedObject(self, &_propertyIsUniqueAssociatedKey) != nil
? true
: entity.isPropertyUnique(self)
objc_getAssociatedObject(self, &AssociatedKeys.propertyIsUniqueAssociatedKey) != nil
? true
: entity.isPropertyUnique(self)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ extension CoreData.NSRelationshipDescription {
extension CoreData.NSRelationshipDescription: SchemaProperty {}

public extension CoreData.NSRelationshipDescription {

@inlinable var isToOneRelationship : Bool { !isToMany }

@inlinable var isAttribute : Bool { return false }
@inlinable var isRelationship : Bool { return true }

Expand Down Expand Up @@ -109,7 +109,7 @@ extension CoreData.NSRelationshipDescription {
// MARK: - Initializer

public extension CoreData.NSRelationshipDescription {

// Note: This matches what the `Relationship` macro takes.
convenience init(_ options: Option..., deleteRule: NSDeleteRule = .nullify,
minimumModelCount: Int? = 0, maximumModelCount: Int? = 0,
Expand All @@ -122,16 +122,16 @@ public extension CoreData.NSRelationshipDescription {
precondition(minimumModelCount ?? 0 >= 0)
precondition(maximumModelCount ?? 0 >= 0)
self.init()

self.name = name ?? ""
self.valueType = valueType
self.renamingIdentifier = originalName ?? ""
self.versionHashModifier = hashModifier
self.deleteRule = deleteRule
self.inverseKeyPath = inverse

if options.contains(.unique) { isUnique = true }

if let minimumModelCount { self.minCount = minimumModelCount }
if let maximumModelCount {
self.maxCount = maximumModelCount
Expand All @@ -140,8 +140,8 @@ public extension CoreData.NSRelationshipDescription {
if valueType is any RelationshipCollection.Type {
self.maxCount = 0
}
else if valueType is NSOrderedSet.Type ||
valueType is Optional<NSOrderedSet>.Type
else if valueType is NSOrderedSet.Type ||
valueType is Optional<NSOrderedSet>.Type
{
self.maxCount = 0
}
Expand All @@ -155,10 +155,8 @@ public extension CoreData.NSRelationshipDescription {

// MARK: - Storage

private var _relationshipInfoAssociatedKey: UInt8 = 72

extension CoreData.NSRelationshipDescription {

func internalCopy() -> Self {
guard let copy = self.copy() as? Self else {
fatalError("Could not copy relationship \(self)")
Expand Down Expand Up @@ -187,7 +185,7 @@ extension CoreData.NSRelationshipDescription {
var inverseName : String?
var destination : String?
var isToOneRelationship : Bool?

override func copy() -> Any { internalCopy() }

func internalCopy() -> MacroInfo {
Expand All @@ -201,10 +199,14 @@ extension CoreData.NSRelationshipDescription {
return copy
}
}


private struct AssociatedKeys {
nonisolated(unsafe) static var relationshipInfoAssociatedKey: Void? = nil
}

var writableRelationshipInfo : MacroInfo {
if let info =
objc_getAssociatedObject(self, &_relationshipInfoAssociatedKey)
objc_getAssociatedObject(self, &AssociatedKeys.relationshipInfoAssociatedKey)
as? MacroInfo
{
return info
Expand All @@ -214,15 +216,15 @@ extension CoreData.NSRelationshipDescription {
self.relationshipInfo = info
return info
}

var relationshipInfo: MacroInfo? {
// Note: isUnique is only used during schema construction!
set {
objc_setAssociatedObject(self, &_relationshipInfoAssociatedKey,
objc_setAssociatedObject(self, &AssociatedKeys.relationshipInfoAssociatedKey,
newValue, .OBJC_ASSOCIATION_RETAIN)
}
get {
objc_getAssociatedObject(self, &_relationshipInfoAssociatedKey)
objc_getAssociatedObject(self, &AssociatedKeys.relationshipInfoAssociatedKey)
as? MacroInfo
}
}
Expand Down

0 comments on commit 6f632da

Please sign in to comment.