gpt4 book ai didi

ios - AFNetworking 2.0 - 当应用程序在后台时,请求不会进入成功 block

转载 作者:可可西里 更新时间:2023-11-01 17:07:33 33 4
gpt4 key购买 nike

  • 我正在为我的项目使用 AFNetworking 2.0。很简单,我只是向服务器发送一些请求,服务器返回一些 JSON我。
  • 为了测试它,我在成功 block 中标记了一些调试标记AFNetworking POST 方法。问题是当应用程序在前台和当应用程序收到服务器的响应时,xCode 可以在成功和失败 block 中的调试标记处停止。但是当我发出请求时,然后将应用程序带到后台(按主页按钮),调试永远不会进入 block 。
  • 他们似乎在应用程序处于后台时暂停了,因为当应用程序再次处于前台时,调试立即进入 block ,我得到了回应。

我错过了什么吗?因为我希望应用程序即使在后台也能收到响应并做一些事情。这是我在我的应用程序中使用的内容:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = /*generate the parameters*/;
NSURL *filePath = [NSURL fileURLWithPath:@"imageName.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);

//debug here
//Told the app to stop the Loading View, save response to DB

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);

//debug here
//Told the app to stop the Loading View and try again sometimes

}];

最佳答案

这是我的错。我忘了告诉应用程序进入后台时继续运行。我将这些代码添加到我的项目中:

UIBackgroundTaskIdentifier backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
}];

当应用程序进入后台时:

[[UIApplicatioz sharedApplication] endBackgroundTask:backgroundTask];

关于ios - AFNetworking 2.0 - 当应用程序在后台时,请求不会进入成功 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970176/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com