Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data not sent from a background via 3G

Tags:

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

-(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 the inputData is a simple dictionary with strings.

A method doRequest is based on a 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 through Wi-Fi, the app goes into background, and after few minutes, a custom bluetooth device sends some data and wakes an application from a suspended mode. After the data has been received by iOS application, it fails to send it to a server if a device is connected over 3G. I am positive that data sent via bluetooth is received because it is stored in local database.

Also there is one other important fact. If an application is run through the Xcode, even if a device is connected over 3G, the application will send a data from background. To do this, I run an app, then tap a Home button to put it into background.

Don't know what is the difference and why app acts differently when it is attached with a cable to Mac, and why data is not sent via 3G (or even 2G) ?

Additional info:

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

like image 613
Whirlwind Avatar asked Nov 08 '16 13:11

Whirlwind


People also ask

How do I enable background data on my iPhone?

Allow background data usage iPhoneGo to Settings and tap General. Select Background App Refresh. Choose which apps you want to enable background data for by toggling the switch from gray to green.

What is background data usage?

To use the Play Store app, you'll need to turn background data on for your device. This means apps may download data for future reference or provide you with notifications even when you're not using the app. Settings are different on each version of Android.

Why is my iPhone not connecting to cellular data?

Go to Settings > Cellular and check to see if your cellular line is turned on. If your cellular line is off, turn it back on. Then check to see if you have service. If you don't see your cellular line in Settings > Cellular, you should set up an eSIM or insert a physical SIM card.

How do I turn off background data on my iPhone?

Open Settings, then tap Connections > Data usage. From the Mobile section, tap Mobile data usage. Select an app from below the usage graph. Turn off Allow background data usage.


1 Answers

It seems likely that this is about power usage. The background upload offered by URL session is a convenience, and it's offered to you at the discression of the OS - it offers to send the data but it gets to choose when.

Things which affect when the data will be sent include wether the device is connected to power, the quality of the data connection (how long and how much power will it take to send the data), what else the device is doing (can it combine multiple uploads)...

So, you can't guess or rely on any task being actioned at any particular time while in the background.

This kind of testing should really be done on a device only, and not connected to Xcode because it influences the test. Instead, use something like Charles proxy to log the network requests and use the device, leaving it for periods of time and maybe opening and using some other app. You should see the data get sent eventually, but you will have to wait.

like image 139
Wain Avatar answered Sep 25 '22 16:09

Wain