ios - Calling AFHTTPSessionManager.downloadTasks in AFNetworking 2 freezes the main thread -
i have singleton class contains afhttpsessionmanager *filetransfersessionmanager. in want cancel downloads, before starting them anew done running through downloadtasks , canceling them. problem is, when downloadtasks attribute called, main thread freezes if there running downloads.
// method canceling download tasks: nsarray *downloadtasks = self.filetransfersessionmanager.downloadtasks; (nsurlsessiondownloadtask *downloadtask in downloadtasks) { [downloadtask cancel]; } // in afurlsessionmanager.m - (nsarray *)downloadtasks { return [self tasksforkeypath:nsstringfromselector(_cmd)]; }
calls method freezes main thread, because gettaskswithcompletionhandler dispatch_semaphore_signal(semaphore) never called:
// in afurlsessionmanager.m - (nsarray *)tasksforkeypath:(nsstring *)keypath { __block nsarray *tasks = nil; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [self.session gettaskswithcompletionhandler:^(nsarray *datatasks, nsarray *uploadtasks, nsarray *downloadtasks) { if ([keypath isequaltostring:nsstringfromselector(@selector(datatasks))]) { tasks = datatasks; } else if ([keypath isequaltostring:nsstringfromselector(@selector(uploadtasks))]) { tasks = uploadtasks; } else if ([keypath isequaltostring:nsstringfromselector(@selector(downloadtasks))]) { tasks = downloadtasks; } else if ([keypath isequaltostring:nsstringfromselector(@selector(tasks))]) { tasks = [@[datatasks, uploadtasks, downloadtasks] valueforkeypath:@"@unionofarrays.self"]; } dispatch_semaphore_signal(semaphore); }]; nslog(@"current thread: %@", [nsthread currentthread]); dispatch_semaphore_wait(semaphore, dispatch_time_forever); return tasks; }
i have tracked problem down self.session gettaskswithcompletionhandler: never entering block, , therefore dispatch_semaphore_wait wait literally forever unlock main thread.
what can cause of this, doubt bug in apples framework, thats why ask here before submitting it. have done obvious mistakes may cause problem?
Comments
Post a Comment