Permalink
Browse files

Changing the self define in BBLock to be weak.

Thanks to @Larcus94 for suggestion this solution!
  • Loading branch information...
1 parent caa4472 commit 195f6289092ac31e9b50c45e3457fade446eec5b @kgn committed Jun 6, 2012
Showing with 23 additions and 4 deletions.
  1. +12 −3 BBlock.h
  2. +11 −1 BBlock.m
View
@@ -8,12 +8,21 @@
#import <Foundation/Foundation.h>
-#define BBlockSelf __block typeof(self)
-#define BBlockTypeOf(o) __block typeof(o)
+/// For when you need a weak reference to self, example: `BBlockWeakSelf wself = self;`
+#define BBlockWeakSelf __weak typeof([self weakReference])
-@interface BBlock : NSObject
+/// For when you need a weak reference of an object, example: `BBlockWeakObject(obj) wobj = obj;`
+#define BBlockWeakObject(o) __weak typeof([o weakReference])
+
+
+@interface BBlock
/// Execute the block on the main thread
+ (void)dispatchOnMainThread:(void (^)())block;
@end
+
+
+@interface NSObject(BBlockWeakReference)
+- (instancetype)weakReference;
+@end
View
@@ -11,10 +11,20 @@
@implementation BBlock
+ (void)dispatchOnMainThread:(void (^)())block{
- NSParameterAssert(block != nil);
+ NSParameterAssert(block != nil);
dispatch_async(dispatch_get_main_queue(), ^{
block();
});
}
@end
+
+
+// Thanks to Larcus94 for suggesting this solution for creating a generic weak self define!
+// http://stackoverflow.com/questions/10892361/generic-typeof-for-weak-self-references#comment14215007_10896047
+@implementation NSObject(BBlockWeakReference)
+- (instancetype)weakReference{
+ __weak id obj = self;
+ return obj;
+}
+@end

0 comments on commit 195f628

Please sign in to comment.