Permalink
Browse files

changed observer passing

  • Loading branch information...
1 parent f96ae9a commit 936db3b8f0bdf258b7c6451f0319b0f11b2dd86e Krzysztof Zabłocki committed Mar 28, 2013
@@ -117,18 +117,18 @@ - (void)sf_addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aNa
//! Add auto remove when observer is going to be deallocated
__AH_WEAK __block id weakSelf = self;
- __AH_WEAK __block id weakObserver = observer;
__AH_WEAK __block id weakObject = anObject;
- void *key = [observer performBlockOnDealloc:^{
+ void *key = [observer performBlockOnDealloc:^(id obj){
+ id strongObserver = obj;
int numberOfRemovals = 0;
- if ((numberOfRemovals = [weakSelf sf_removeObserver:weakObserver name:aName object:weakObject registeredNotifications:registeredNotifications])) {
+ if ((numberOfRemovals = [weakSelf sf_removeObserver:strongObserver name:aName object:weakObject registeredNotifications:registeredNotifications])) {
for (int i = 0; i < numberOfRemovals; ++i) {
[weakSelf setAllowMethodForwarding:YES];
#if SF_OBSERVERS_LOG_ORIGINAL_METHODS
- NSLog(@"Calling original method %@ with parameters %@ %@ %@", NSNotificationCenterSFObserversRemoveSpecificSelector, weakObserver, aName, weakObject);
+ NSLog(@"Calling original method %@ with parameters %@ %@ %@", NSNotificationCenterSFObserversRemoveSpecificSelector, strongObserver, aName, weakObject);
#endif
- objc_msgSend(weakSelf, NSSelectorFromString(NSNotificationCenterSFObserversRemoveSpecificSelector), weakObserver, aName, weakObject);
+ objc_msgSend(weakSelf, NSSelectorFromString(NSNotificationCenterSFObserversRemoveSpecificSelector), strongObserver, aName, weakObject);
[weakSelf setAllowMethodForwarding:NO];
}
}
@@ -116,17 +116,17 @@ - (void)sf_addObserver:(id)observer forKeyPath:(NSString *)keyPath options:(NSKe
//! Add auto remove when observer is going to be deallocated
__AH_WEAK __block id weakSelf = self;
- __AH_WEAK __block id weakObserver = observer;
- void *key = [observer performBlockOnDealloc:^{
+ void *key = [observer performBlockOnDealloc:^(id obj){
+ id strongObserver = obj;
int numberOfRemovals = 0;
- if ((numberOfRemovals = [weakSelf sf_removeObserver:weakObserver forKeyPath:keyPath context:aContext registeredKeyPaths:registeredKeyPaths])) {
+ if ((numberOfRemovals = [weakSelf sf_removeObserver:strongObserver forKeyPath:keyPath context:aContext registeredKeyPaths:registeredKeyPaths])) {
for (int i = 0; i < numberOfRemovals; ++i) {
[weakSelf setAllowMethodForwarding:YES];
#if SF_OBSERVERS_LOG_ORIGINAL_METHODS
- NSLog(@"Calling original method %@ with parameters %@ %@ %p", NSObjectKVOSFObserversRemoveSpecificSelector, weakObserver, keyPath, aContext);
+ NSLog(@"Calling original method %@ with parameters %@ %@ %p", NSObjectKVOSFObserversRemoveSpecificSelector, strongObserver, keyPath, aContext);
#endif
- objc_msgSend(weakSelf, NSSelectorFromString(NSObjectKVOSFObserversRemoveSpecificSelector), weakObserver, keyPath, aContext);
+ objc_msgSend(weakSelf, NSSelectorFromString(NSObjectKVOSFObserversRemoveSpecificSelector), strongObserver, keyPath, aContext);
[weakSelf setAllowMethodForwarding:NO];
}
}
@@ -66,7 +66,7 @@
@interface NSObject (SFExecuteOnDealloc)
#if SF_EXECUTE_ON_DEALLOC_USE_SHORTHAND
-- (void *)performBlockOnDealloc:(void(^)(void))aBlock;
+- (void *)performBlockOnDealloc:(void (^)(id))aBlock;
- (void)cancelDeallocBlockWithKey:(void *)blockKey;
#else
@@ -10,20 +10,21 @@
#import "SFObservers.h"
@interface SFExecuteOnDeallocInternalObject : NSObject
-@property(nonatomic, copy) void (^block)();
+@property(nonatomic, copy) void (^block)(id);
+@property(nonatomic, assign) __unsafe_unretained id obj;
-- (id)initWithBlock:(void(^)(void))aBlock;
+- (id)initWithBlock:(void (^)(id))aBlock;
@end
@implementation SFExecuteOnDeallocInternalObject {
- void(^block)(void);
+ void(^block)(id);
}
@synthesize block;
-- (id)initWithBlock:(void(^)(void))aBlock
+- (id)initWithBlock:(void (^)(id))aBlock
{
self = [super init];
if (self) {
@@ -35,7 +36,7 @@ - (id)initWithBlock:(void(^)(void))aBlock
- (void)dealloc
{
if (block) {
- block();
+ block(_obj);
AH_RELEASE(block);
}
AH_SUPER_DEALLOC;
@@ -44,18 +45,21 @@ - (void)dealloc
@implementation NSObject (SFExecuteOnDealloc)
#if SF_EXECUTE_ON_DEALLOC_USE_SHORTHAND
-- (void *)performBlockOnDealloc:(void(^)(void))aBlock
+- (void *)performBlockOnDealloc:(void (^)(id))aBlock
#else
- (void*)sf_performBlockOnDealloc:(void(^)(void))aBlock
#endif
{
//! we need some object that will be deallocated with this one, and since we are only assigning and never again needing access to this object, let use its memory adress as key
SFExecuteOnDeallocInternalObject *internalObject = [[SFExecuteOnDeallocInternalObject alloc] initWithBlock:aBlock];
+ internalObject.obj = self;
objc_setAssociatedObject(self, AH_BRIDGE(internalObject), internalObject, OBJC_ASSOCIATION_RETAIN);
AH_RELEASE(internalObject);
return AH_BRIDGE(internalObject);
}
+
#if SF_EXECUTE_ON_DEALLOC_USE_SHORTHAND
+
- (void)cancelDeallocBlockWithKey:(void *)blockKey
#else
- (void)sf_cancelDeallocBlockWithKey:(void*)blockKey
@@ -10,6 +10,6 @@
@interface SFAppDelegate : UIResponder <UIApplicationDelegate>
-@property (strong, nonatomic) UIWindow *window;
+@property(strong, nonatomic) UIWindow *window;
@end
@@ -14,11 +14,11 @@ @implementation SFAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
+ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
+ // Override point for customization after application launch.
+ self.window.backgroundColor = [UIColor whiteColor];
+ [self.window makeKeyAndVisible];
+ return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
@@ -1,45 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
-<dict>
- <key>CFBundleDevelopmentRegion</key>
- <string>en</string>
- <key>CFBundleDisplayName</key>
- <string>${PRODUCT_NAME}</string>
- <key>CFBundleExecutable</key>
- <string>${EXECUTABLE_NAME}</string>
- <key>CFBundleIdentifier</key>
- <string>pl.pixle.${PRODUCT_NAME:rfc1034identifier}</string>
- <key>CFBundleInfoDictionaryVersion</key>
- <string>6.0</string>
- <key>CFBundleName</key>
- <string>${PRODUCT_NAME}</string>
- <key>CFBundlePackageType</key>
- <string>APPL</string>
- <key>CFBundleShortVersionString</key>
- <string>1.0</string>
- <key>CFBundleSignature</key>
- <string>????</string>
- <key>CFBundleVersion</key>
- <string>1.0</string>
- <key>LSRequiresIPhoneOS</key>
- <true/>
- <key>UIRequiredDeviceCapabilities</key>
- <array>
- <string>armv7</string>
- </array>
- <key>UISupportedInterfaceOrientations</key>
- <array>
- <string>UIInterfaceOrientationPortrait</string>
- <string>UIInterfaceOrientationLandscapeLeft</string>
- <string>UIInterfaceOrientationLandscapeRight</string>
- </array>
- <key>UISupportedInterfaceOrientations~ipad</key>
- <array>
- <string>UIInterfaceOrientationPortrait</string>
- <string>UIInterfaceOrientationPortraitUpsideDown</string>
- <string>UIInterfaceOrientationLandscapeLeft</string>
- <string>UIInterfaceOrientationLandscapeRight</string>
- </array>
-</dict>
+ <dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>en</string>
+ <key>CFBundleDisplayName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>pl.pixle.${PRODUCT_NAME:rfc1034identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+ <key>LSRequiresIPhoneOS</key>
+ <true/>
+ <key>UIRequiredDeviceCapabilities</key>
+ <array>
+ <string>armv7</string>
+ </array>
+ <key>UISupportedInterfaceOrientations</key>
+ <array>
+ <string>UIInterfaceOrientationPortrait</string>
+ <string>UIInterfaceOrientationLandscapeLeft</string>
+ <string>UIInterfaceOrientationLandscapeRight</string>
+ </array>
+ <key>UISupportedInterfaceOrientations~ipad</key>
+ <array>
+ <string>UIInterfaceOrientationPortrait</string>
+ <string>UIInterfaceOrientationPortraitUpsideDown</string>
+ <string>UIInterfaceOrientationLandscapeLeft</string>
+ <string>UIInterfaceOrientationLandscapeRight</string>
+ </array>
+ </dict>
</plist>
@@ -11,6 +11,8 @@
#ifdef __OBJC__
#define SF_OBSERVERS_LOG_ORIGINAL_METHODS 1
#define SF_OBSERVERS_ALLOW_MULTIPLE_REGISTRATIONS 1
- #import <UIKit/UIKit.h>
- #import <Foundation/Foundation.h>
+
+#import <UIKit/UIKit.h>
+#import <Foundation/Foundation.h>
+
#endif
@@ -10,9 +10,8 @@
#import "SFAppDelegate.h"
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
@autoreleasepool {
- return UIApplicationMain(argc, argv, nil, NSStringFromClass([SFAppDelegate class]));
+ return UIApplicationMain(argc, argv, nil, NSStringFromClass([SFAppDelegate class]));
}
}
@@ -45,10 +45,10 @@ - (void)testKVOAutoRemovalWithContext
- (void)testKVOAutoRemovalWithIntegerContext
{
- static NSInteger SomeIntegerContext;
- [observedObject addObserver:observer forKeyPath:@"description" options:NSKeyValueObservingOptionNew context:&SomeIntegerContext];
- AH_RELEASE(observer), observer = nil;
- AH_RELEASE(observedObject), observedObject = nil;
+ static NSInteger SomeIntegerContext;
+ [observedObject addObserver:observer forKeyPath:@"description" options:NSKeyValueObservingOptionNew context:&SomeIntegerContext];
+ AH_RELEASE(observer), observer = nil;
+ AH_RELEASE(observedObject), observedObject = nil;
}
- (void)testKVOAutoRemovalMultiple

0 comments on commit 936db3b

Please sign in to comment.