因此,我正在尝试处理向服务器发布内容,这可以在iPhone上网时使用。现在我需要处理请求,如果没有互联网。我尝试了一些代码来处理与可达性相关的操作队列。我不认为我需要缓存,因为我发帖。我在这里错过了什么?我希望在互联网可用时尽快发送请求。虽然我可能在应用程序中做了其他事情。我唯一怀疑的是我的OperationManager在每个请求中初始化,如POST或GET,所以也许前一个可能是wipeout。但那是操作管理器而不是操作队列。
- (void)sendRequestPOST:(NSString *)url parameters:(NSMutableDictionary *)parameters {
NSLog(@"POST Request URL : %@", url);
NSLog(@"POST Request Body : %@", parameters);
__block AFHTTPRequestOperation *opr;
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *jsonRS = [[AFJSONRequestSerializer alloc] init];
[manager setRequestSerializer:jsonRS];
[[manager requestSerializer] setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[[manager requestSerializer] setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[[manager responseSerializer] setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/html", nil]];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
}];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES];
break;
}
}];
[manager.reachabilityManager startMonitoring];
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
[MBProgressHUD hideHUDForView:self.mainView animated:YES];
opr = operation;
[self.mainView setUserInteractionEnabled:YES];
NSLog(@"POST Response : %@",responseObject);
NSLog(@"Status Code : %ld",(long)[[operation response] statusCode]);
if([_delegate respondsToSelector:@selector(didFinish:dictInfo:)]) {
[AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
[_delegate didFinish:self dictInfo:(NSMutableDictionary *)responseObject];
NSLog(@"Request finished");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
opr = operation;
[MBProgressHUD hideHUDForView:self.mainView animated:YES];
[self.mainView setUserInteractionEnabled:YES];
NSLog(@"POST Error: %@", error);
// [[AFHTTPRequestOperationManager manager].operatonQueue addOperation:operation];
if([_delegate respondsToSelector:@selector(failRequest:statusCode:)]) {
[AFNetworkActivityIndicatorManager sharedManager].enabled = NO;
[_delegate failRequest:self statusCode:[[operation response] statusCode]];
NSLog(@"Request failed");
}
}];}
答案 0 :(得分:0)
我编写了一个Swift pod,它允许网络请求以一种无论当前网络连接如何排队的方式进行封装,并保存到它之前保存 - https://cocoapods.org/pods/OfflineRequestManager。对于任何看到这个的人都可能有用。
最简单的用例看起来如下所示,尽管大多数实际情况(保存到磁盘,特定请求数据等)都会有更多的环节要跳过:
import OfflineRequestManager
class SimpleRequest: OfflineRequest {
func perform(completion: @escaping (Error?) -> Void) {
doMyNetworkRequest(withCompletion: { response, error in
handleResponse(response)
completion(error)
})
}
}
///////
OfflineRequestManager.defaultManager(queueRequest: SimpleRequest())