由于AFNetworking 2.0在AFHTTPClient已经消失的青睐两位经理的:AFHTTPRequestOperationManager和AFHTTPSessionManager.迁移指南几乎没有说明每种情况下最好使用的情况.据我所知,与RESTful的基本交互JSON API可以分别使用它们中的每一个来实现.
什么是最合适的情况使用RequestOperationManager或SessionManager?
objective-c afhttpclient afhttprequestoperation afnetworking-2
我正在通过Google Places API在搜索栏上集成自动填充功能.对于网络请求,我使用AFNetworking.
我想一次只运行一个请求.所以每次我输入一封新信,我都会做以下事情:
1 - 取消之前的AFHTTPRequestOperation:
[self.currentGlobalSearchListRequest cancel];
NSLog(@"%@ cancelled", self.currentGlobalSearchListRequest);
Run Code Online (Sandbox Code Playgroud)
2 - 开始新的AFHTTPRequestOperation:
self.currentGlobalSearchListRequest = [searchService getGlobalSearchListItemsForString:searchString inRegion:region delegate:self];
NSLog(@"%@ started", self.currentGlobalSearchListRequest);
Run Code Online (Sandbox Code Playgroud)
这是在请求完成运行时调用的回调:
- (void)request:(PointSearchRequest *)request didLoadSearchListItems:(NSArray *)searchListItems {
NSAssert(request == self.currentGlobalSearchListRequest, @"!!!callback from a request that should be cancelled!!!");
[self.delegate searchListLoader:self didLoadGlobalSearchList:searchListItems];
}
Run Code Online (Sandbox Code Playgroud)
有时候我打了断言,所以我调查了一下,发现大多数时候用一个错误代码来调用失败块,NSURLErrorCancelled这是预期的行为,但有时会调用成功块!
这个堆栈跟踪告诉我,我的代码中的内容以正确的顺序发生
2013-12-22 09:38:46.484 Point[63595:a0b] <PointSearchRequest: 0x18202b50> started
2013-12-22 09:38:46.486 Point[63595:a0b] <PointSearchRequest: 0x18202b50> cancelled
2013-12-22 09:38:46.487 Point[63595:a0b] <PointSearchRequest: 0x181a5dd0> started
2013-12-22 09:38:46.496 Point[63595:a0b] *** Assertion failure in -[SearchListLoader request:didLoadSearchListItems:], /Users/aurelienporte/Documents/Developpement/Perso/iOS/Point/Point/Classes/Models/SearchListLoader.m:82
(lldb) po …Run Code Online (Sandbox Code Playgroud) 在更新我的应用程序以支持后台应用程序刷新时,我遇到了AFNetworking问题。
我越来越NSPOSIXErrorDomain Code=53 "Software caused connection abort"。该问题似乎发生在iOS 12中,其中后台连接被终止。
AFNetworking 2.6.3用于进行提取。
AppDelegate.m:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[OrdersService performFetch];
completionHandler(UIBackgroundFetchResultNewData);
}
Run Code Online (Sandbox Code Playgroud)
OrdersService.m:
-(void) performFetch {
[[AFHTTPRequestOperationManager new] GET:@"https://www.example.com/orders"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
];
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
[错误] GET'(null)'(0)[31.9163 s]:错误域= NSPOSIXErrorDomain代码= 53“软件导致连接中止” UserInfo = {NSErrorFailingURLStringKey = https://www.example.com/orders,_kCFStreamErrorDomainKey = 1 ,NSErrorPeerAddressKey = {长度= 16,容量= 16,字节= 0x100201bb3e80187c0000000000000000},_ kCFStreamErrorCodeKey = 53,NSErrorFailingURLKey = https://www.example.com/orders }
ios afnetworking afhttprequestoperation afnetworking-2 ios12
我的服务器正在生成签名上传到 S3 的信息。当我将该信息传递到 iOS 时,它适用于小文件。有时它也适用于较大的,但经常会超时。我目前正在通过 WIFI 在 iOS 7 上进行测试。对于大约 35 MB 的文件,它通常会在大约 60% 后超时,但有时它会完成,有时会更快超时。
我的服务器生成以下信息,我将其作为表单部分传递给我的 POST。(我也尝试过将其作为参数,结果类似):
以及一个 URL。
这是我生成上传的代码:
AFHTTPRequestOperation *op = [manager POST:url
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if( throttle )
[formData throttleBandwidthWithPacketSize:kAFUploadStream3GSuggestedPacketSize delay:kAFUploadStream3GSuggestedDelay];
for( id key in parameters ) {
NSString *val = parameters[key];
[formData appendPartWithFormData:[val dataUsingEncoding:NSUTF8StringEncoding] name:key];
}
NSError *error;
if( ![formData appendPartWithFileURL:videoUrl name:@"file" fileName:@"movie.mov" mimeType:@"video/quicktime" error:&error] ) {
// handle the error
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) …Run Code Online (Sandbox Code Playgroud) 我有这个快速的代码,我认为应该工作,但它无法处理客观的静态字段.我试图添加"()"多个地方但没有任何作用.
func AFHTTPRequestOperationManagerDispatchOnMainQueue(mainQueue: Bool) -> AFHTTPRequestOperationManager {
var manager = AFHTTPRequestOperationManager.manager
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)
manager.requestSerializer = AFJSONRequestSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)
if (!mainQueue) {
manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
}
return manager;
}
Run Code Online (Sandbox Code Playgroud)
字段和属性如下所示:
+ (instancetype)manager;
@property (nonatomic, strong) AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer;
@property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;
Run Code Online (Sandbox Code Playgroud)
我收到这些错误消息:
错误:'manager()'不可用:使用对象构造'AFHTTPRequestOperationManager()'
错误:'() - > AFHTTPRequestOperationManager!' 没有名为'responseSerializer'的成员
错误:'() - > AFHTTPRequestOperationManager!' 没有名为'requestSerializer'的成员
错误:'() - > AFHTTPRequestOperationManager!' 没有名为'completionQueue'的成员
错误:函数产生预期类型'AFHTTPRequestOperationManager!'; 你的意思是用'()'来称呼它?
我正在使用AFNetworking下载使用第三方类解析的数据.我曾经多次使用AFNetworking做类似的动作,但出于某种原因,当我调用downloadProgressBlock并进行计算以使用我的进度条时,数字会返回负数.见下文:
2013-09-22 16:04:06.036 Portland Police[2228:60b] Download = -31849.000000
2013-09-22 16:04:06.041 Portland Police[2228:60b] Download = -40537.000000
2013-09-22 16:04:06.042 Portland Police[2228:60b] Download = -44881.000000
2013-09-22 16:04:06.044 Portland Police[2228:60b] Download = -53569.000000
2013-09-22 16:04:06.046 Portland Police[2228:60b] Download = -62257.000000
2013-09-22 16:04:06.048 Portland Police[2228:60b] Download = -63705.000000
2013-09-22 16:04:06.085 Portland Police[2228:60b] Download = -70945.000000
2013-09-22 16:04:06.087 Portland Police[2228:60b] Download = -89769.000000
2013-09-22 16:04:06.089 Portland Police[2228:60b] Download = -94113.000000
2013-09-22 16:04:06.100 Portland Police[2228:60b] Download = -98457.000000
2013-09-22 16:04:06.104 Portland Police[2228:60b] Download = -102801.000000
2013-09-22 16:04:06.111 …Run Code Online (Sandbox Code Playgroud) 我试图从iPad使用afnetworking从服务器下载文件.
我可以成功下载文件.但是,奇怪的是,虽然totalBytesRead是正确的,但我的totalBytesExpectedToRead总是-1.我哪里弄错了?
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
}
Run Code Online (Sandbox Code Playgroud)
整个代码在这里.
-(void)fileDownload : (NSString *)pathToDownload : (NSString *)fileExt
{
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText=@"Downloading...";
NSLog(@"fileDownload and pathToDownload is %@",pathToDownload);
NSArray *parts = [pathToDownload componentsSeparatedByString:@"\\"];
NSString *fileName = [NSString stringWithFormat:@"%@",[parts objectAtIndex:[parts count]-1]];
if([fileExt isEqualToString:@"pdf"])
{
fileNameForAllThreeFile=[fileName stringByDeletingPathExtension];
}
NSLog(@"fileName is %@",fileName);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_ADDRESS,download_recording]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithUsername:usernameForAuth password:passwordForAuth];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
pathToDownload,@"file",
nil];
NSMutableURLRequest …Run Code Online (Sandbox Code Playgroud) 我更新xcode版本8.0(8A218a)swift 3后,我收到此错误
无法将类型'(AFHTTPRequestOperation?,AnyObject?) - >()'的值转换为预期的参数类型'((AFHTTPRequestOperation?,Any?) - > Void)!'
这是以下显示错误的代码.
jsonmanager.post( "http://myapi.com",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
if(responseObject.object(forKey: "meta")?.object(forKey: "status")?.intValue == 200){....
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么 ?
它在以前的版本7.3.1 swift 2中运行良好.
ios ×6
afnetworking ×4
objective-c ×3
afhttpclient ×2
swift ×2
amazon-s3 ×1
ios10 ×1
ios12 ×1
post ×1
progress-bar ×1
swift3 ×1