objective c - Run Request In Background IOS -


i want run request while app in background.my app supports background mode -> background fetches.

but below code return failure while app in background! there way run request?i tried nsurlconnection receives response not receive response!

 [[afhttprequestoperationmanager manager]post:@"http://app.xxxx.com/message_delivery.php" parameters:@{@"message_id":msgid} constructingbodywithblock:^(id<afmultipartformdata> formdata) {       } success:^(afhttprequestoperation *operation, id response) {         nslog(@"succesfuly tik");     } failure:^(afhttprequestoperation *operation, nserror *error) {          nslog(@"%@",error);     }]; 

these code working not

  nsurl *url=[nsurl urlwithstring:[nsstring stringwithformat:@"http://app.xxxx.com/message_delivery.php?message_id=%@",msgid]];     nsurlrequest *request = [nsurlrequest requestwithurl:url                                               cachepolicy:nsurlrequestuseprotocolcachepolicy                                           timeoutinterval:2];     [[[nsurlconnection alloc] initwithrequest:request delegate:self]start]; 

background fetch specific technology lets os opportunistically call code, in turn calls web service see if there new data retrieve. don't have time perform request (30 seconds?). it's not intended substantive (or if is, you'd marry background fetch background nsurlsession, different technology). , when background fetch 1 of these opportunistic calls, call completion handler when you're done.

but 2 code samples don't show calling completion handler passed app delegate's performfetchwithcompletionhandler. i'm unclear whether you're doing background fetch , haven't shared relevant code, or whether you're conflating different technologies.

there 3 basic background networking approaches, each own pros , cons.

  • there background fetch, os initiate opportunistic network request @ time of own choosing;

  • there beginbackgroundtaskwithexpirationhandler, lets app continue running few minutes (nowadays 3 minutes) finish finite length tasks ... useful if you're starting network request know may take few minutes , don't want die because user left app; ,

  • there nsurlsession background nsurlsessionconfiguration, in ios daemon performs requests on behalf of app , lets upload , download requests might take longer (even after app has been terminated), waking app when done.

it's not clear me, on basis of description, of these applicable in case. when requests running in background, how precisely requests initiated , how long take, worst case scenario?

having said that, when doing background networking, consider shifting nsurlsession, opens additional opportunities. afnetworking, achieved afhttpsessionmanager (rather afhttprequestoperationmanager; it's little complicated see https://stackoverflow.com/a/21359684/1271826 discussion of configuring afnetworking background sessions). if doing yourself, it's using nsurlsession background nsurlsessionconfiguration rather using nsurlconnection.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -