iphone - NSURLConnection data received after connectionDidFinishLoading -
i have question nsurlconnection: want download image with:
nsurlconnection *theconnection=[[nsurlconnection alloc] initwithrequest:urlrequest delegate:self];
the first delegate called (correctly) is
- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response
after called 1 time:
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data
and immediately:
- (void)connectiondidfinishloading:(nsurlconnection *)connection
up here correct, after connectiondidfinishloading fired 1 time 1 time again didreceivedata delegate same connection, identify connection by:
nsstring * connectionkey = [[[connection originalrequest] url] absolutestring];
is possible?
update, more infos:
my app fire many simultaneous connections , store infos connection in dictionary, when delegate called retrive connection info using key: nsstring * connectionkey = [[[connection originalrequest] url] absolutestring]; 99% of connections right 1 (avery time same!) connection have behavior
here finish implementation:
- (void)downloadfilefromurl:(nsurl *)url inpath:(nsstring *)completefilepath datareceivedblock:(void (^)(long long bytedownloaded ,long long totalbyte))datareceivedblock endblock:(void (^)(nsstring * downloadpath, nsdictionary * responseheaders))endblock failblock:(void (^)(nsstring * downloadpath, nsdictionary * responseheaders, nserror * error))failblock { //save connection infos nsmutabledictionary * requestdata = [[nsmutabledictionary alloc] init]; if(datareceivedblock) [requestdata setobject:[datareceivedblock copy] forkey:@"datareceivedblock"]; if(endblock) [requestdata setobject:[endblock copy] forkey:@"endblock"]; if(failblock) [requestdata setobject:[failblock copy] forkey:@"failblock"]; [requestdata setobject:[nsnumber numberwithbool:yes] forkey:@"usingblock"]; [requestdata setobject: completefilepath forkey:@"downloaddestinationpath"]; //delete file if on fs if([[nsfilemanager defaultmanager] fileexistsatpath: completefilepath]) [[nsfilemanager defaultmanager] removeitematpath:completefilepath error:nil]; // create request. nsurlrequest * urlrequest = [nsurlrequest requestwithurl:url cachepolicy:nsurlcachestorageallowed timeoutinterval:time_out]; nsurlconnection *theconnection=[[nsurlconnection alloc] initwithrequest:urlrequest delegate:self]; if (!theconnection) { // inform user connection failed. failblock(completefilepath,nil,[nserror errorwithdomain:@"connection fail" code:0 userinfo:nil]); } //add connection infos requests dictionary nsstring * connectionkey = [[[theconnection originalrequest] url] absolutestring]; [self.requests setobject:requestdata forkey:connectionkey]; }
here delegate example:
- (void)connectiondidfinishloading:(nsurlconnection *)connection { nsstring * connectionkey = [[[connection originalrequest] url] absolutestring]; nsmutabledictionary * requestdata = [self.requests objectforkey:connectionkey]; nsfilehandle *file = [requestdata objectforkey:@"filehandle"]; [file closefile]; //se la richiesta usa o no block bool usingblock = [[requestdata objectforkey:@"usingblock"] boolvalue]; if(usingblock) { __block void (^endblock)(nsstring * , nsdictionary *) = [requestdata objectforkey:@"endblock"]; if(endblock) endblock([requestdata objectforkey:@"downloaddestinationpath"],[requestdata objectforkey:@"responseheaders"]); } else [self.delegate downloadended:[requestdata objectforkey:@"responseheaders"]]; //elimino dati richiesta [self.requests removeobjectforkey:[nsstring stringwithformat:@"%@",connectionkey]]; //inibisco standby [uiapplication sharedapplication].idletimerdisabled = no; }
it's impossible. it's must different connection callback.
ensure each connection utilize unique delegate, or unique switch branch in same delegate.
iphone ios objective-c nsurlconnection nsurlconnectiondelegate
No comments:
Post a Comment