i want download 3 files in serial order. 2 of them txt files , 1 .gz file. using nsurlconnection download above files.
i new ios programming. have seen in other question in , google can use serial dispatch queue operation serially.
but don't know how nsurlconnection. tried below did not work.
dispatch_queue_t serialqueue = dispatch_queue_create("com.clc.propqueue", dispatch_queue_serial); dispatch_async(serialqueue, ^{ [self downloadprop]; }); dispatch_async(serialqueue, ^{ [self downloaddatabase]; }); dispatch_async(serialqueue, ^{ [self downloadtxt]; }); above code not executing connectiondidfinishloading of nsurlconnection. has idea how achieve this?
nsurlsession provides queue download each task in order in created.
nsurlsession *session = [nsurlsession sharedsession]; nsurlsessiontask *task1 = [session datataskwithurl:[nsurl urlwithstring:@"http://yahoo.com"] completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nslog(@"complete 1"); }]; nsurlsessiontask *task2 = [session datataskwithurl:[nsurl urlwithstring:@"http://msn.com"] completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nslog(@"complete 2"); }]; nsurlsessiontask *task3 = [session datataskwithurl:[nsurl urlwithstring:@"http://google.com"] completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nslog(@"complete 3"); }]; // regardless of order tasks "resumed" (aka started) execute synchronously in order added, above. [task3 resume]; [task1 resume]; [task2 resume]; update based on comments & chat:
to more deterministic on ordering & execution of tasks...
nsurlsession *session = [nsurlsession sharedsession]; __block nsurlsessiontask *task1 = nil; __block nsurlsessiontask *task2 = nil; __block nsurlsessiontask *task3 = nil; task1 = [session datataskwithurl:urltofirstfile completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { // check error nslog(@"first file completed downloading"); [task2 resume]; }]; task2 = [session datataskwithurl:urltosecondfile completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { // check error nslog(@"second file completed downloading"); [task3 resume]; }]; task3 = [session datataskwithurl:[nsurl urlwithstring:@"http://google.com"] completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { // check error nslog(@"third file completed downloading"); }]; [task1 resume];
Comments
Post a Comment