我正在将一个块传递给异步方法,该方法稍后会执行此块。如果我在将块传递给someMethod之前没有复制块,我的应用程序崩溃了:成功:失败:
有没有办法在forwardInvocation中复制块:而不是在将它传递给someMethod之前复制它:success:failure:?
流程是 someMethod:成功:失败: - > forwardInvocation: - > HTTPGET:成功:失败
httpGet:success:failure:根据HTTP状态代码执行成功或失败块。
// 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
你能帮忙吗?
答案 0 :(得分:3)
罪魁祸首是[invocation retainArguments]
行。如果你注释掉那一行,它就可以了。 (无论如何,这条线从来都不是必需的,因为从不以异步方式存储或使用调用。)
说明:
考虑一下-retainArguments
的作用。它在对象指针类型的所有参数上调用retain
。然后,当取消分配调用时,它会调用release
。
但参数是堆栈(非复制)块。 retain
和release
对它没有影响(因为它不是堆对象)。因此,当它被保留时,没有任何反应,然后(从崩溃中)看来调用在某个时刻被自动释放(完全正常的事情发生),因此调用的最终释放和释放是异步发生的。当取消分配调用时,它会尝试释放其保留的参数,但到那时它会尝试发送一个不再有效的堆栈块,从而导致崩溃。
P.S。复制forwardInvocation:
中的块也是不必要的