revert recent swizzling update. sorry about that

This commit is contained in:
John Estropia
2017-02-10 20:33:07 +09:00
parent 19fea6953a
commit 03973790a8
7 changed files with 232 additions and 222 deletions

View File

@@ -31,210 +31,211 @@ import CoreData
internal extension NSManagedObject {
@nonobjc
internal static func cs_swizzleMethodsForLogging() {
struct Static {
static let isSwizzled = Static.swizzle()
private static func swizzle() -> Bool {
NSManagedObject.cs_swizzle(
original: #selector(NSManagedObject.willAccessValue(forKey:)),
proxy: #selector(NSManagedObject.cs_willAccessValue(forKey:))
)
NSManagedObject.cs_swizzle(
original: #selector(NSManagedObject.willChangeValue(forKey:)),
proxy: #selector(NSManagedObject.cs_willChangeValue(forKey:))
)
NSManagedObject.cs_swizzle(
original: #selector(NSManagedObject.willChangeValue(forKey:withSetMutation:using:)),
proxy: #selector(NSManagedObject.cs_willChangeValue(forKey:withSetMutation:using:))
)
return true
}
}
assert(Static.isSwizzled)
}
@nonobjc
private static func cs_swizzle(original originalSelector: Selector, proxy swizzledSelector: Selector) {
let originalMethod = class_getInstanceMethod(NSManagedObject.self, originalSelector)
let swizzledMethod = class_getInstanceMethod(NSManagedObject.self, swizzledSelector)
let didAddMethod = class_addMethod(
NSManagedObject.self,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)
)
if didAddMethod {
class_replaceMethod(
NSManagedObject.self,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod)
)
}
else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
private dynamic func cs_willAccessValue(forKey key: String?) {
self.cs_willAccessValue(forKey: key)
guard CoreStore.logger.enableObjectConcurrencyDebugging else {
return
}
guard let context = self.managedObjectContext else {
CoreStore.log(
.warning,
message: "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(NSManagedObjectContext.self))."
)
return
}
if context.isTransactionContext {
guard let transaction = context.parentTransaction else {
CoreStore.log(
.warning,
message: "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) after has been deleted from its transaction."
)
return
}
CoreStore.assert(
transaction.isRunningInAllowedQueue(),
"Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) outside its transaction's designated queue."
)
return
}
if context.isDataStackContext {
guard context.parentStack != nil else {
CoreStore.log(
.warning,
message: "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(DataStack.self)).")
return
}
CoreStore.assert(
Thread.isMainThread,
"Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) outside the main thread."
)
return
}
}
private dynamic func cs_willChangeValue(forKey key: String?) {
self.cs_willChangeValue(forKey: key)
guard CoreStore.logger.enableObjectConcurrencyDebugging else {
return
}
guard let context = self.managedObjectContext else {
CoreStore.log(
.warning,
message: "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(NSManagedObjectContext.self))."
)
return
}
if context.isTransactionContext {
guard let transaction = context.parentTransaction else {
CoreStore.log(
.warning,
message: "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) after has been deleted from its transaction."
)
return
}
CoreStore.assert(
transaction.isRunningInAllowedQueue(),
"Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) outside its transaction's designated queue."
)
return
}
if context.isDataStackContext {
guard context.parentStack != nil else {
CoreStore.log(
.warning,
message: "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(DataStack.self)).")
return
}
CoreStore.assert(
Thread.isMainThread,
"Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) outside the main thread."
)
return
}
}
private dynamic func cs_willChangeValue(forKey inKey: String, withSetMutation inMutationKind: NSKeyValueSetMutationKind, using inObjects: Set<AnyHashable>) {
self.cs_willChangeValue(
forKey: inKey,
withSetMutation: inMutationKind,
using: inObjects
)
guard CoreStore.logger.enableObjectConcurrencyDebugging else {
return
}
guard let context = self.managedObjectContext else {
CoreStore.log(
.warning,
message: "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(NSManagedObjectContext.self))."
)
return
}
if context.isTransactionContext {
guard let transaction = context.parentTransaction else {
CoreStore.log(
.warning,
message: "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) after has been deleted from its transaction."
)
return
}
CoreStore.assert(
transaction.isRunningInAllowedQueue(),
"Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) outside its transaction's designated queue."
)
return
}
if context.isDataStackContext {
guard context.parentStack != nil else {
CoreStore.log(
.warning,
message: "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(DataStack.self)).")
return
}
CoreStore.assert(
Thread.isMainThread,
"Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) outside the main thread."
)
return
}
}
// TODO: test before release (rolled back)
// @nonobjc
// internal static func cs_swizzleMethodsForLogging() {
//
// struct Static {
//
// static let isSwizzled = Static.swizzle()
//
// private static func swizzle() -> Bool {
//
// NSManagedObject.cs_swizzle(
// original: #selector(NSManagedObject.willAccessValue(forKey:)),
// proxy: #selector(NSManagedObject.cs_willAccessValue(forKey:))
// )
// NSManagedObject.cs_swizzle(
// original: #selector(NSManagedObject.willChangeValue(forKey:)),
// proxy: #selector(NSManagedObject.cs_willChangeValue(forKey:))
// )
// NSManagedObject.cs_swizzle(
// original: #selector(NSManagedObject.willChangeValue(forKey:withSetMutation:using:)),
// proxy: #selector(NSManagedObject.cs_willChangeValue(forKey:withSetMutation:using:))
// )
// return true
// }
// }
// assert(Static.isSwizzled)
// }
//
// @nonobjc
// private static func cs_swizzle(original originalSelector: Selector, proxy swizzledSelector: Selector) {
//
// let originalMethod = class_getInstanceMethod(NSManagedObject.self, originalSelector)
// let swizzledMethod = class_getInstanceMethod(NSManagedObject.self, swizzledSelector)
// let didAddMethod = class_addMethod(
// NSManagedObject.self,
// originalSelector,
// method_getImplementation(swizzledMethod),
// method_getTypeEncoding(swizzledMethod)
// )
// if didAddMethod {
//
// class_replaceMethod(
// NSManagedObject.self,
// swizzledSelector,
// method_getImplementation(originalMethod),
// method_getTypeEncoding(originalMethod)
// )
// }
// else {
//
// method_exchangeImplementations(originalMethod, swizzledMethod)
// }
// }
//
// private dynamic func cs_willAccessValue(forKey key: String?) {
//
// self.cs_willAccessValue(forKey: key)
//
// guard CoreStore.logger.enableObjectConcurrencyDebugging else {
//
// return
// }
//
// guard let context = self.managedObjectContext else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(NSManagedObjectContext.self))."
// )
// return
// }
// if context.isTransactionContext {
//
// guard let transaction = context.parentTransaction else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) after has been deleted from its transaction."
// )
// return
// }
// CoreStore.assert(
// transaction.isRunningInAllowedQueue(),
// "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) outside its transaction's designated queue."
// )
// return
// }
// if context.isDataStackContext {
//
// guard context.parentStack != nil else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(DataStack.self)).")
// return
// }
// CoreStore.assert(
// Thread.isMainThread,
// "Attempted to access the \"\(key ?? "")\" key of an object of type \(cs_typeName(self)) outside the main thread."
// )
// return
// }
// }
//
// private dynamic func cs_willChangeValue(forKey key: String?) {
//
// self.cs_willChangeValue(forKey: key)
//
// guard CoreStore.logger.enableObjectConcurrencyDebugging else {
//
// return
// }
//
// guard let context = self.managedObjectContext else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(NSManagedObjectContext.self))."
// )
// return
// }
// if context.isTransactionContext {
//
// guard let transaction = context.parentTransaction else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) after has been deleted from its transaction."
// )
// return
// }
// CoreStore.assert(
// transaction.isRunningInAllowedQueue(),
// "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) outside its transaction's designated queue."
// )
// return
// }
// if context.isDataStackContext {
//
// guard context.parentStack != nil else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(DataStack.self)).")
// return
// }
// CoreStore.assert(
// Thread.isMainThread,
// "Attempted to change the \"\(key ?? "")\" of an object of type \(cs_typeName(self)) outside the main thread."
// )
// return
// }
// }
//
// private dynamic func cs_willChangeValue(forKey inKey: String, withSetMutation inMutationKind: NSKeyValueSetMutationKind, using inObjects: Set<AnyHashable>) {
//
// self.cs_willChangeValue(
// forKey: inKey,
// withSetMutation: inMutationKind,
// using: inObjects
// )
//
// guard CoreStore.logger.enableObjectConcurrencyDebugging else {
//
// return
// }
//
// guard let context = self.managedObjectContext else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(NSManagedObjectContext.self))."
// )
// return
// }
// if context.isTransactionContext {
//
// guard let transaction = context.parentTransaction else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) after has been deleted from its transaction."
// )
// return
// }
// CoreStore.assert(
// transaction.isRunningInAllowedQueue(),
// "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) outside its transaction's designated queue."
// )
// return
// }
// if context.isDataStackContext {
//
// guard context.parentStack != nil else {
//
// CoreStore.log(
// .warning,
// message: "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) after has been deleted from its \(cs_typeName(DataStack.self)).")
// return
// }
// CoreStore.assert(
// Thread.isMainThread,
// "Attempted to mutate the \"\(inKey)\" of an object of type \(cs_typeName(self)) outside the main thread."
// )
// return
// }
// }
}