Data not sent from the background via 3G

I have an application that sends data to the server while the application is in the background. Here is the code responsible for sending the data:

-(bool) sendStats: (MCStatsSender*) val{ if(![self checkInternet]){ //Using Reachability here return false; } NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys: self.propertyA.value, "key1", val.data, "key2", nil]; [myNetworkManager doRequest:[myRequestManager createWithStringAndDictionary:MY_URL Data:inputData handler:myHandler user:val]]; return true; } 

So inputData is a simple dictionary with strings.

The doRequest method doRequest based on NSURLSession and basically looks like this:

 -(void) doRequest: (MCRequest*) request{ [tasks addObject:request]; if(m_session == nil){ NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"key-%lu",reqid]]; m_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; } NSURLSessionDataTask* task = [m_session dataTaskWithRequest:request.generatedRequest]; request.reqId = task.taskIdentifier; [task resume]; } 

As I said, everything works via Wi-Fi , the application goes into the background, and after a few minutes the bluetooth user device sends some data and wakes the application from paused mode. After the data has been received by the iOS application, it cannot send it to the server if the device is connected via 3G. I am sure that the data sent via bluetooth has been received since it is stored in a local database.

There is also another important fact. If the application is launched via Xcode, even if the device is connected via 3G, the application will send data from the background. To do this, I launched the application, and then click the "Home" button to place it in the background.

I don’t know what the difference is and why the application works differently when it is connected via cable to the Mac, and why data is not sent via 3G (or even 2G)?

Additional Information:

I am not trying to upload the file, but just send JSON to the server.

+5
source share
1 answer

It seems that this is about using energy. The background download offered by the URL session is convenient and is offered to you when the OS is disproportionate - it offers to send data, but it can choose when.

Things that affect sending data include: the device is connected to a power source, the quality of the data connection (how much time and how much energy it takes to send the data), what else does the device do (can it combine several downloads) ...

Thus, you cannot guess or rely on any task that will be performed at any particular time in the background.

This type of testing should really only be performed on the device and is not related to Xcode, since it affects the test. Instead, use something like a Charles proxy to log network requests and use the device, leaving it for a while and possibly opening and using another application. You should see that the data will be sent in the end, but you will have to wait.

+4
source

Source: https://habr.com/ru/post/1259463/


All Articles