我将一个 block 传递给一个稍后执行该 block 的异步方法。如果我在将 block 传递给 someMethod:success:failure:
之前不复制 block ,我的应用程序会崩溃有没有办法复制 forwardInvocation: 中的 block ,而不是在将其传递给 someMethod:success:failure: 之前复制它?
流程是someMethod:success:failure: -> forwardInvocation: -> httpGet:success:failure
httpGet:success:failure:根据 HTTP 状态代码执行成功或失败 block 。
// AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) id response;
@property (strong, nonatomic) NSError *error;
@end
// AppDelegate.m
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// The app crashes if the blocks are not copied here!
[[MyController new] someMethod:[^(NSString *response) {
self.response = response;
NSLog(@"response = %@", response);
} copy] failure:[^(NSError *error) {
self.error = error;
} copy]];
return YES;
}
@end
// MyController.h
@protocol MyControllerProtocol <NSObject>
@optional
- (void)someMethod:(void (^)(NSString *response))success
failure:(void (^)(NSError *error))failure;
@end
@interface MyController : NSObject <MyControllerProtocol>
@end
// MyController.m
#import "MyController.h"
#import "HTTPClient.h"
@implementation MyController
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation retainArguments];
NSUInteger numberOfArguments = [[invocation methodSignature] numberOfArguments];
typedef void(^SuccessBlock)(id object);
typedef void(^FailureBlock)(NSError *error);
__unsafe_unretained SuccessBlock successBlock1;
__unsafe_unretained SuccessBlock failureBlock1;
[invocation getArgument:&successBlock1 atIndex:(numberOfArguments - 2)]; // success block is always the second to last argument (penultimate)
SuccessBlock successBlock = [successBlock1 copy];
[invocation getArgument:&failureBlock1 atIndex:(numberOfArguments - 1)]; // failure block is always the last argument
FailureBlock failureBlock = [failureBlock1 copy];
NSLog(@"successBlock copy = %@", successBlock);
NSLog(@"failureBlock copy = %@", failureBlock);
// Simulates a HTTP request and calls the success block later!
[HTTPClient httpGet:@"somerequest" success:successBlock failure:failureBlock];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
NSMethodSignature *methodSignature = [super methodSignatureForSelector:sel];
return methodSignature;
}
@end
// HTTPClient.h
@interface HTTPClient : NSObject
+ (void)httpGet:(NSString *)path
success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure;
@end
// HTTPClient.m
#import "HTTPClient.h"
@implementation HTTPClient
+ (void)httpGet:(NSString *)path
success:(void (^)(id object))success
failure:(void (^)(NSError *error))failure
{
// Invoke the method.
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(),
^{
success(@"foo");
});
}
@end
完整的源代码可以在这里找到:https://github.com/priteshshah1983/BlocksWithNSInvocation
你能帮忙吗?
最佳答案
罪魁祸首是 [invocation retainArguments]
行。如果您注释掉该行,它就可以正常工作。 (无论如何,该行从来都不是必需的,因为调用永远不会被异步存储或使用。)
解释:
想想 -retainArguments
做了什么。它对对象指针类型的所有参数调用 retain
。然后当调用被解除分配时,它会调用它们的 release
。
但参数是堆栈(非复制) block 。 retain
和 release
对它没有影响(因为它不是堆对象)。因此,当它被保留时,什么也没有发生,然后(从崩溃中)看起来调用在某个时候被自动释放(发生的完全正常的事情),所以调用的最终释放和释放是异步发生的。当调用被解除分配时,它会尝试释放其保留的参数,但此时它会尝试向不再有效的堆栈 block 发送消息,从而导致崩溃。
附言forwardInvocation:
中的 block 的复制也是不必要的
关于objective-c - 将 block 传递给异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17606802/