简体   繁体   English

当应用程序进入后台时,在 iOS 设备的 Flutter 应用程序上发布请求失败

[英]Post request failing on flutter app for iOS device when app goes to background

I am requesting a post call from my flutter application.我正在请求我的 flutter 应用程序的后调用。

 try {
      String token = await getToken();
      Map<String, String> headers = {
        'Content-Type': 'application/json',
        'Authorization': token
      };
      response = await http
          .post(encodedUri, headers: headersAuth ?? headers, body: requestBody)
          .timeout(const Duration(seconds: timeout));
    } on SocketException {
      Log.logError('Socket Error while calling post API');
      throw FetchDataException('No Internet Connection');
    } catch (e, s) { 
      throw FetchDataException('Error while calling post call');
    }

User has the option to put the application in background after doing this call.执行此调用后,用户可以选择将应用程序置于后台。 While doing that call I am getting error like this from dart IOClient在进行该调用时,我从 dart IOClient 收到了这样的错误

Error: Bad file descriptor

The stack trace is following堆栈跟踪如下

#0      IOClient.send (package:http/src/io_client.dart:66)
<asynchronous suspension>
#1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:93)
<asynchronous suspension>
#2      _withClient (package:http/http.dart:164)
<asynchronous suspension>
#3      Future.timeout.<anonymous closure> (dart:async/future_impl.dart:870)
<asynchronous suspension>
#4      ApiService.post (package:fdgv2/services/api_service.dart:121)
<asynchronous suspension>

This is happening only for release build not in debug build.这只发生在发布版本而不是调试版本中。 On xcode console following more errors are thrown.在 xcode 控制台上抛出更多错误。

2022-08-02 15:53:35.039710+0530 Runner[8357:850399] [connection] nw_read_request_report [C14] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.041675+0530 Runner[8357:850399] [connection] nw_read_request_report [C14] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.044130+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Formulas ran on reading launch
2022-08-02 15:53:35.048650+0530 Runner[8357:850399] [connection] nw_read_request_report [C1] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.051303+0530 Runner[8357:850399] [connection] nw_read_request_report [C1] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.055034+0530 Runner[8357:850399] [connection] nw_read_request_report [C3] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.056821+0530 Runner[8357:850403] flutter: Cancel succeeded for Operation: 7f32f82e-526b-43c3-97cf-3ace22894c40
2022-08-02 15:53:35.057394+0530 Runner[8357:850399] [connection] nw_read_request_report [C3] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.058136+0530 Runner[8357:850403] flutter: Cancel succeeded for Operation: db013087-d948-45ce-a43e-374b0d9ae0c3
2022-08-02 15:53:35.063756+0530 Runner[8357:850399] [connection] nw_read_request_report [C18] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.064509+0530 Runner[8357:850399] [connection] nw_read_request_report [C18] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.066183+0530 Runner[8357:850399] [connection] nw_read_request_report [C18] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.071824+0530 Runner[8357:850399] [connection] nw_read_request_report [C11] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.077681+0530 Runner[8357:850399] [connection] nw_read_request_report [C11] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.080873+0530 Runner[8357:850399] [connection] nw_read_request_report [C8] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.081923+0530 Runner[8357:850399] [connection] nw_read_request_report [C8] Receive failed with error "Socket is not connected"
2022-08-02 15:53:35.085330+0530 Runner[8357:850399] [connection] nw_read_request_report [C17] Receive failed with error "Software caused connection abort"
2022-08-02 15:53:35.086249+0530 Runner[8357:850399] [connection] nw_read_request_report [C13] Receive failed with error "Software caused connection abort"
2022-08-02 15:53:35.088127+0530 Runner[8357:850399] [] nw_flow_service_writes Failing write request <nw_write_request> [57: Socket is not connected]
2022-08-02 15:53:35.144915+0530 Runner[8357:850399] [connection] nw_write_request_report [C18] Send failed with error "Socket is not connected"
2022-08-02 15:53:35.199598+0530 Runner[8357:850399] Connection 18: write error 1:57
2022-08-02 15:53:35.208024+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C14.1.1:2] [-c95faba0f1ce339c] unable to request outbound data
2022-08-02 15:53:35.226047+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C1.1.1:2] [-c5ccd1eb1de55d67] unable to request outbound data
2022-08-02 15:53:35.263606+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C3.1.1:2] [-1e6494b1e41a8c90] unable to request outbound data
2022-08-02 15:53:35.266920+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C18.1.1:2] [-3326233f689377a5] unable to request outbound data
2022-08-02 15:53:35.267424+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C18.1.1:2] [-3326233f689377a5] unable to request outbound data
2022-08-02 15:53:35.293100+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C11.1.1:2] [-726354f32e176e2e] unable to request outbound data
2022-08-02 15:53:35.330543+0530 Runner[8357:850399] [quic] quic_send_frames_for_key_state_block_invoke [C8.1.1:2] [-886b86481a89ccec] unable to request outbound data
2022-08-02 15:53:35.341767+0530 Runner[8357:850399] Task <D40EAFA0-D987-4A96-9076-CFD41D41C68A>.<2> HTTP load failed, 1150/0 bytes (error code: -1005 [1:57])
2022-08-02 15:53:35.347143+0530 Runner[8357:850399] Connection 18: received failure notification
2022-08-02 15:53:35.354038+0530 Runner[8357:850399] [connection] nw_endpoint_handler_unregister_context [C18.1.1 34.107.204.85:443 failed channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, ipv6, dns)] Cannot unregister after flow table is released
2022-08-02 15:53:35.354133+0530 Runner[8357:850415] Task <D40EAFA0-D987-4A96-9076-CFD41D41C68A>.<2> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=57, NSUnderlyingError=0x280111f50 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x282d578e0 [0x1f475c1b8]>{length = 16, capacity = 16, bytes = 0x100201bb226bcc550000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <D40EAFA0-D987-4A96-9076-CFD41D41C68A>.<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <D40EAFA0-D987-4A96-9076-CFD41D41C68A>.<2>"
), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://data.pendo.io/v2/devices/analyticsData, NSErrorFailingURLKey=https://data.pendo.io/v2/devices/analyticsData, _kCFStreamErrorDomainKey=1}
2022-08-02 15:53:41.320830+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - App on inactive mode by user
2022-08-02 15:53:41.321376+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Closing current reading
2022-08-02 15:53:41.326543+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Current Reading closed
2022-08-02 15:53:41.329393+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Initialized formula and carry forward manager
2022-08-02 15:53:41.330445+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Reading updated with data for 2022-08-01
2022-08-02 15:53:41.330958+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Attribute View Model updated with data from Attribute Presenter Model
2022-08-02 15:53:41.340179+0530 Runner[8357:850403] flutter: Info: 8/2/2022 3:53 PM - Formulas ran on reading launch
2022-08-02 15:53:41.340451+0530 Runner[8357:850403] flutter: called dismiss
2022-08-02 15:53:41.341208+0530 Runner[8357:850403] flutter: Error: 8/2/2022 3:53 PM - Error while calling Post API https://api-fdg-qa.joynai.net/mobile/api/rpc/readingdata/interpolate 
Error: Bad file descriptor 

Strangely enough, if do a get call and put the iOS device in background it is still getting successful.奇怪的是,如果调用 get 并将 iOS 设备置于后台,它仍然会成功。 But for post call I am getting this error.但是对于邮寄电话,我收到了这个错误。 Can any one please help me with this?谁能帮我解决这个问题?

As you might know, connections between clients and servers are usually kept alive up to x amount of time.您可能知道,客户端和服务器之间的连接通常会保持活动状态长达 x 的时间。 What Bad file descriptor means is that your device thinks the connection is still alive and it's trying to perform a call within a connection which at some point in time was closed. Bad file descriptor的意思是您的设备认为连接仍然存在,并且它正在尝试在某个时间点关闭的连接内执行调用。

I am no expert, but my guess is that when the app is put in background all connections get closed.我不是专家,但我的猜测是,当应用程序置于后台时,所有连接都会关闭。 This doesn't necessarily mean you can't perform any call with the app in background.这并不一定意味着您不能在后台使用该应用程序执行任何呼叫。

Long story short, this might be a race-condition issue: your app is trying to perform a call with a broken state (Thinking a connection is still in used).长话短说,这可能是一个竞争条件问题:您的应用程序正在尝试执行中断状态的调用(认为连接仍在使用中)。 Try to delay the call by X amount of milliseconds, this will allow the app to understand that your last connection closed and it will need to open a new one to perform that specific call.尝试将呼叫延迟 X 毫秒,这将使应用程序了解您的最后一个连接已关闭,并且需要打开一个新连接才能执行该特定呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM