Raghav
Raghav

Reputation: 645

NSURLConnection App Crash when the device is locked

I am testing my application on iOS 9.0.2 device. I have the service to upload the data from the device. When the upload is in foreground it works fine. But the same when the upload is running in the background and the device is auto locked (pin security) the it stop and crashed.

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Subtype: unknown at 0x0000000105eaa000
Triggered by Thread:  7

Filtered syslog:
None found

Thread 7 name:  Dispatch queue: com.apple.NSURLSession-work
Thread 7 Crashed:
0   CoreFoundation                  0x0000000184f51730 0x184e64000 + 972592
1   CoreFoundation                  0x0000000184f50e44 0x184e64000 + 970308
2   CoreFoundation                  0x0000000184e68000 0x184e64000 + 16384
3   CFNetwork                       0x000000018481c09c 0x18465c000 + 1835164
4   CFNetwork                       0x00000001846d997c 0x18465c000 + 514428
5   CFNetwork                       0x00000001847e1814 0x18465c000 + 1595412
6   CFNetwork                       0x0000000184783144 0x18465c000 + 1208644
7   CFNetwork                       0x0000000184777548 0x18465c000 + 1160520
8   CFNetwork                       0x000000018477ec9c 0x18465c000 + 1191068
9   CFNetwork                       0x0000000184776528 0x18465c000 + 1156392
10  libdispatch.dylib               0x000000019a3797b0 0x19a378000 + 6064
11  libdispatch.dylib               0x000000019a379770 0x19a378000 + 6000
12  libdispatch.dylib               0x000000019a38575c 0x19a378000 + 55132
13  libdispatch.dylib               0x000000019a37d274 0x19a378000 + 21108
14  libdispatch.dylib               0x000000019a38762c 0x19a378000 + 63020
15  libdispatch.dylib               0x000000019a38734c 0x19a378000 + 62284
16  libsystem_pthread.dylib         0x000000019a58d478 0x19a58c000 + 5240
17  libsystem_pthread.dylib         0x000000019a58d028 0x19a58c000 + 4136

When I do a debugging it will point at a memory location in the debugger which is associated with the NSURLConnection call.

The issue is observed only when the device is passcode is turned on. If it is turned off and user locks the phone it will upload the file without any error.

Help appreciated.

Upvotes: 1

Views: 376

Answers (2)

Vijay Sanghavi
Vijay Sanghavi

Reputation: 340

NSURLConnection does not work in background until you set it to work on background thread. Even after running the same in background you'll get max of 3 minutes to finish your task else OS will suspend the process.

Upvotes: 1

Mike Gledhill
Mike Gledhill

Reputation: 29171

I had kind of the same problem with my iPhone app. I would kick off a background thread to download some data from my web service, but if the device went into idle mode, it would kill the network connection.

The solution, of course, was to set idleTimerDisabled to prevent the device from going to sleep, while it's busy syncing data.

But even then, it worked really badly, even in iOS 8.

My nasty little solution was to set up a timer, to repeatedly prevent the device from going to sleep:

iPhone - phone goes to sleep even if idleTimerDisabled is YES

So, coming back to your Question, I did need to run my download function in the foreground thread, and specifically prevent the device from going to sleep.

Hope this helps.

Upvotes: 0

Related Questions