Skip to content

Instantly share code, notes, and snippets.

@eJamesLin
Created April 26, 2017 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eJamesLin/1f60cb3c16e8eaf4958265eb6c9c954f to your computer and use it in GitHub Desktop.
Save eJamesLin/1f60cb3c16e8eaf4958265eb6c9c954f to your computer and use it in GitHub Desktop.
//
// ViewController.m
// RunLoop
//
// Created by cj on 2017/4/26.
// Copyright © 2017年 cj. All rights reserved.
//
#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google1.com"]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection == nil) {
NSLog(@"Request failed");
} else {
NSLog(@"Request sent");
}
[[NSRunLoop currentRunLoop] run]; // make the delegate method to get called
NSLog(@"first roonloop end");
});
dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^ {
NSMutableURLRequest * request = [ [ NSMutableURLRequest alloc ] initWithURL: [NSURL URLWithString:@"https://www.google1.com"] ];
[ request setHTTPMethod: @"GET" ];
AFHTTPRequestOperation * requestOperation = [ [ AFHTTPRequestOperation alloc ] initWithRequest: request ];
[ requestOperation setCompletionBlockWithSuccess: ^ ( AFHTTPRequestOperation * operation, id responseObject ) {
NSLog(@"%s success", __PRETTY_FUNCTION__);
} failure: ^ ( AFHTTPRequestOperation * operation, NSError * error ) {
NSLog(@"%s fail", __PRETTY_FUNCTION__);
} ];
[ requestOperation start ];
// no need for this currentRunLoop run, the block will still get involved
// [ [ NSRunLoop currentRunLoop ] run ];
} );
dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^ {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(foo:)
userInfo:nil
repeats:NO];
NSLog(@"%s %@", __PRETTY_FUNCTION__, timer);
[[NSRunLoop currentRunLoop] run]; // needed to let foo run successfully
/*
Puts the receiver into a permanent loop, during which time it processes data from all attached input sources.
If no input sources or timers are attached to the run loop, this method exits immediately; otherwise, it runs the receiver in the NSDefaultRunLoopMode by repeatedly invoking runMode:beforeDate:. In other words, this method effectively begins an infinite loop that processes data from the run loop’s input sources and timers.
*/
NSLog(@"RunLoop End");
});
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)foo:(id)sender
{
NSLog(@"%s", __PRETTY_FUNCTION__);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment