From b1972b82f127cd4839ee4f9a4e521333345c56bf Mon Sep 17 00:00:00 2001 From: John Estropia Date: Tue, 18 Apr 2017 11:29:16 +0900 Subject: [PATCH] added way to store userInfo in DataStack and in transactions --- Sources/Setup/DataStack.swift | 39 +++++++++++++++++ .../Transactions/BaseDataTransaction.swift | 43 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/Sources/Setup/DataStack.swift b/Sources/Setup/DataStack.swift index eb42b7a..cad1e2f 100644 --- a/Sources/Setup/DataStack.swift +++ b/Sources/Setup/DataStack.swift @@ -442,6 +442,43 @@ public final class DataStack: Equatable { } + // MARK: 3rd Party Utilities + + /** + Allow external libraries to store custom data in the `DataStack`. App code should rarely have a need for this. + ``` + enum Static { + static var myDataKey: Void? + } + CoreStore.defaultStack[userInfoKey: &Static.myDataKey] = myObject + ``` + - Important: Do not use this method to store thread-sensitive data. + - parameter userInfoKey: the key for custom data. Make sure this is a static pointer that will never be changed. + - returns: A custom data identified by `userInfoKey` + */ + public subscript(userInfoKey key: UnsafeRawPointer) -> Any? { + + get { + + self.userInfoLock.lock() + defer { + + self.userInfoLock.unlock() + } + return self.userInfo[key] + } + set { + + self.userInfoLock.lock() + defer { + + self.userInfoLock.unlock() + } + self.userInfo[key] = newValue + } + } + + // MARK: Equatable public static func == (lhs: DataStack, rhs: DataStack) -> Bool { @@ -571,6 +608,8 @@ public final class DataStack: Equatable { private var persistentStoresByFinalConfiguration = [String: NSPersistentStore]() private var finalConfigurationsByEntityIdentifier = [EntityIdentifier: Set]() + private var userInfo: [UnsafeRawPointer: Any] = [:] + private let userInfoLock = NSRecursiveLock() deinit { diff --git a/Sources/Transactions/BaseDataTransaction.swift b/Sources/Transactions/BaseDataTransaction.swift index baefdf6..9406e0c 100644 --- a/Sources/Transactions/BaseDataTransaction.swift +++ b/Sources/Transactions/BaseDataTransaction.swift @@ -438,6 +438,43 @@ public /*abstract*/ class BaseDataTransaction { } + // MARK: 3rd Party Utilities + + /** + Allow external libraries to store custom data in the transaction. App code should rarely have a need for this. + ``` + enum Static { + static var myDataKey: Void? + } + transaction[userInfoKey: &Static.myDataKey] = myObject + ``` + - Important: Do not use this method to store thread-sensitive data. + - parameter userInfoKey: the key for custom data. Make sure this is a static pointer that will never be changed. + - returns: A custom data identified by `userInfoKey` + */ + public subscript(userInfoKey key: UnsafeRawPointer) -> Any? { + + get { + + self.userInfoLock.lock() + defer { + + self.userInfoLock.unlock() + } + return self.userInfo[key] + } + set { + + self.userInfoLock.lock() + defer { + + self.userInfoLock.unlock() + } + self.userInfo[key] = newValue + } + } + + // MARK: Internal internal let context: NSManagedObjectContext @@ -476,4 +513,10 @@ public /*abstract*/ class BaseDataTransaction { return self.bypassesQueueing || self.transactionQueue.cs_isCurrentExecutionContext() } + + + // MARK: Private + + private var userInfo: [UnsafeRawPointer: Any] = [:] + private let userInfoLock = NSRecursiveLock() }