나중에이 블록을 실행하는 비동기 메서드에 블록을 전달하고 있습니다. someMethod에 전달하기 전에 블록을 복사하지 않으면 내 응용 프로그램이 중단됩니다. 성공 : 실패 :블록을 비동기 메서드에 전달
forwardInvocation의 블록을 복사하는 대신 someMethod에 전달하기 전에 복사하는 대신 success : failure : ?
흐름은 것으로 someMethod : 성공 : 오류 -> forwardInvocation -> httpGet : 성공 : 실패
httpGet : 성공 : 불량 : 성공 또는 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당신이 도와 주실 수 있습니까?
someMethod를 참조해야합니다. 실패 : 코드의 어느 부분에도 나타나지 않습니다. – kball
@kball someMethod : success : failure : 구현되지 않았습니다. 따라서 methodSignatureForSelector :와 forwardInvocation :이 호출됩니다. – pshah
@kball 일부 오타가 수정되었습니다. 죄송합니다. 그것은 어떤 방법으로 생각됩니다 : 성공 : 실패 : – pshah