Guest User

Untitled

a guest
Jan 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  2. dispatch_async(concurrentQueue, ^{
  3.  
  4. __block NSMutableArray *newImages;
  5.  
  6. dispatch_sync(concurrentQueue, ^{
  7. newImages = [NSMutableArray array];
  8. // images retrieved using `NSURLConnection sendSynchronousRequest`
  9. });
  10.  
  11. dispatch_sync(dispatch_get_main_queue(), ^{
  12. // display images to the user
  13. });
  14. });
  15.  
  16. q = /* custom serial queue */
  17. urls = /* urls array */;
  18. NSMutableArray *images = [NSMutableArray new];
  19. for (NSURL *url in URLs) {
  20. NSURLRequest *req = [self imageRequestForURL:url];
  21. dispatch_async(q, ^{
  22. UIImage *image = [self imageFromRequest:req];
  23. [images addObject:newImage];
  24. }
  25. }
  26. dispatch_async(q, ^{
  27. dispatch_async(dispatch_get_main_queue(), ^{
  28. [self applyUpdatesForURLs:urls withImages:images];
  29. });
  30. }
  31.  
  32. NSURL *url = [NSURL URLWithString:@"your server address"];
  33. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
  34. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *theData, NSError *error){
  35. if (error == nil) {
  36. //do what you want with theData and update the UI
  37. }
  38. }];
Add Comment
Please, Sign In to add comment