|
|
|
@ -813,6 +813,10 @@ public class StoreCenter { |
|
|
|
self._deleteLogs.addOrUpdate(instance: dataLog) |
|
|
|
self._deleteLogs.addOrUpdate(instance: dataLog) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the appropriate store for a relationship |
|
|
|
|
|
|
|
/// - Parameters: |
|
|
|
|
|
|
|
/// - instance: some Storable instance |
|
|
|
|
|
|
|
/// - relationship: the relationship |
|
|
|
func relationshipStore<T: Storable>(instance: T, relationship: Relationship) -> Store? { |
|
|
|
func relationshipStore<T: Storable>(instance: T, relationship: Relationship) -> Store? { |
|
|
|
switch relationship.storeLookup { |
|
|
|
switch relationship.storeLookup { |
|
|
|
case .main: return Store.main |
|
|
|
case .main: return Store.main |
|
|
|
@ -821,8 +825,13 @@ public class StoreCenter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func hasDirectReference<T: Storable, S: Storable>(instance: T, relationshipType: S.Type, relationship: Relationship) -> Bool { |
|
|
|
/// Returns if an instance has at least one valid parent relationship by checking if the id of the parent exists |
|
|
|
if let referenceId = instance[keyPath: relationship.keyPath] as? S.ID, let store = self.relationshipStore(instance: instance, relationship: relationship) { |
|
|
|
/// - Parameters: |
|
|
|
|
|
|
|
/// - instance: some Storable instance |
|
|
|
|
|
|
|
/// - relationship: the relationship |
|
|
|
|
|
|
|
func hasParentReferences<T: Storable, S: Storable>(instance: T, relationshipType: S.Type, relationship: Relationship) -> Bool { |
|
|
|
|
|
|
|
if let referenceId = instance[keyPath: relationship.keyPath] as? S.ID, |
|
|
|
|
|
|
|
let store = self.relationshipStore(instance: instance, relationship: relationship) { |
|
|
|
let instance: S? = store.findById(referenceId) |
|
|
|
let instance: S? = store.findById(referenceId) |
|
|
|
return instance != nil |
|
|
|
return instance != nil |
|
|
|
} |
|
|
|
} |
|
|
|
@ -830,12 +839,14 @@ public class StoreCenter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func isReferenced<T: Storable>(instance: T) -> Bool { |
|
|
|
func isReferenced<T: Storable>(instance: T) -> Bool { |
|
|
|
let relationships = T.relationships() |
|
|
|
|
|
|
|
for relationship in relationships { |
|
|
|
for relationship in T.parentRelationships() { |
|
|
|
if self.hasDirectReference(instance: instance, relationshipType: relationship.type, relationship: relationship) { |
|
|
|
if self.hasParentReferences(instance: instance, relationshipType: relationship.type, relationship: relationship) { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for relationship in T.childrenRelationships() { |
|
|
|
if let store = self.relationshipStore(instance: instance, relationship: relationship) { |
|
|
|
if let store = self.relationshipStore(instance: instance, relationship: relationship) { |
|
|
|
if store.isReferenced(collectionType: relationship.type, type: T.self, id: instance.stringId) { |
|
|
|
if store.isReferenced(collectionType: relationship.type, type: T.self, id: instance.stringId) { |
|
|
|
return true |
|
|
|
return true |
|
|
|
|