1
我有一個被調用的方法,它所做的第一件事是確定網絡何時可到達。如果不是,我想等10s,然後用最初傳入的參數/參數運行相同的方法。重試Objective-C方法,直到滿足條件
這是我的災難性嘗試(我更像一個JS開發人員,相對較新Objective-C的):
- (void)sendRequestToURL:(NSString *)url withPostData:(NSString *)postData withPage:(int)page sortBy:(NSString *)sort completionBlock:(void (^)(NSDictionary *))completion {
if(![FooNetworkManager isReachable]){
self.lastRequest = ??? // lastRequest is an NSDictionary
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 10);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
[self sendRequestToURL:self.lastRequest]; // this is almost definitely wrong
});
}
}
在JavaScript中,一個方法裏面,我們有機會獲得包含被傳入方法的所有PARAMS的arguments
對象,不知道如何在Objective-C複製此。
此外,self.lastRequest
定義進一步向上在這同一類:以這種方式你只是捕捉
- (void)sendRequestToURL:(NSString *)url withPostData:(NSString *)postData withPage:(int)page sortBy:(NSString *)sort completionBlock:(void (^)(NSDictionary *))completion {
if(![FooNetworkManager isReachable]){
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 10);
dispatch_after(delay, dispatch_get_main_queue(), ^(void){
[self sendRequestToURL:url withPostData:postData withPage:page sortBy:sort completionBlock:completion];
});
}
}
:
@property(nonatomic, strong) NSDictionary *lastRequest;
不知道爲什麼我沒有這樣做!?謝謝。 – benhowdle89
我的回答錯了,這是正確的。對不起@ benhowdle89! – dokun1