ios - AFNetworking. check dowload progress for all operationqueue -


i have ios application made afnetworking. have singleton custom httpclient subclass , use consume api , download binary files server.

i need dowload 100 files. safe iterate trough url array , create 1 afhttprequestionoperation each url? code this:

nsmutableurlrequest* rq = [[mihttpclient sharedinstance] requestwithmethod:@"get" path:@"...." parameters:nil]; afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:rq] ; operation.outputstream = [nsoutputstream outputstreamtofileatpath:path append:no];  [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) {     //completion  } failure:^(afhttprequestoperation *operation, nserror *error) {     //manage error }];   [[mihttpclient sharedinstance] enqueuehttprequestoperation:operation]; 

how can receive feedback queue? don't see "httpclientdelegate" protocol or this.

thanks

regarding progress, see setdownloadprogressblock (part of afurlconnectionoperation, afhttprequestoperation subclassed), e.g.:

[operation setdownloadprogressblock:^(nsinteger byteswritten, nsinteger totalbyteswritten, nsinteger totalbytesexpectedtowrite) {     nslog(@"sent %d of %d bytes, %@", totalbyteswritten, totalbytesexpectedtowrite, path); }]; 

and, in code sample, you're calling setcompletionblockwithsuccess:failure:, provides status regarding completion of individual operations. personally, in little test, maintain array of requested downloads, , have these 3 blocks (progress, success, , failure) update status code of downloads so:

for (nsinteger = 0; < 20; i++) {     nsurlrequest *request = ... // set request accordingly      // create download object keep track of status of download      downloadobject *object = [[downloadobject alloc] init];     download.title = [nsstring stringwithformat:@"download %d", i];     download.status = kdownloadobjectstatusnotstarted;     [self.downloadobjects addobject:download];      afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request];      [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) {         download.status = kdownloadobjectstatusdonesucceeded;          // update ui, example, have table view 1 row per download         //         // [self.tableview reloadrowsatindexpaths:@[[nsindexpath indexpathforrow:i insection:0]]         //                       withrowanimation:uitableviewrowanimationnone];     } failure:^(afhttprequestoperation *operation, nserror *error) {         download.status = kdownloadobjectstatusdonefailed;          // update ui         //         // [self.tableview reloadrowsatindexpaths:@[[nsindexpath indexpathforrow:i insection:0]]         //                       withrowanimation:uitableviewrowanimationnone];     }];      [operation setdownloadprogressblock:^(nsuinteger bytesread, long long totalbytesread, long long totalbytesexpectedtoread) {         download.totalbytesread = totalbytesread;         download.status = kdownloadobjectstatusinprogress;          // update ui         //         // [self.tableview reloadrowsatindexpaths:@[[nsindexpath indexpathforrow:i insection:0]]         //                       withrowanimation:uitableviewrowanimationnone];     }];      [queue addoperation:operation]; } 

you can keep track of individual downloads way. can keep track of pending operations (or query httpclient object's operationqueue property, , @ operationcount property of that).


in terms of downloading 100 files, 2 considerations:

  • i have thought you'd want call setmaxconcurrentoperationcount on operationqueue of httpclient subclass, setting reasonable number (4 or 5), glancing @ afnetworking code, doesn't seem that. find there diminishing returns if go above that, , if of files single server, there constraints on how many concurrent operations can done between given client , given server.

  • i've read anecdotal claims apple reject apps make extraordinary requests on cellular network. see https://stackoverflow.com/a/14922807/1271826.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -