iOS-Bug: NSRunloop Mode

static NSString *CNSDataRunloopMode = @"CNSDataRunloppMode";

@implementation CNSData
{
    NSURLConnection *_connection;
    NSString *_runLoopMode;
}
@synthesize data = _data;
@synthesize finished = _finished;

- (id)initWithUrl:(NSURL *)url {
    self = [super init];
    if (self) {
        _data = [NSMutableData data];
        _runLoopMode = CNSDataRunloopMode;
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
        [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
        _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
        [_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:_runLoopMode]; //mark
        [_connection start];
        while (!self.finished) {
            [[NSRunLoop currentRunLoop] runMode:_runLoopMode beforeDate:[NSDate distantFuture]]; //mark
        }
    }
    return self;
}


wrong:

static NSString *CNSDataRunloopMode = @"CNSDataRunloppMode";

@implementation CNSData
{
    NSURLConnection *_connection;
    NSString *_runLoopMode;
}
@synthesize data = _data;
@synthesize finished = _finished;

- (id)initWithUrl:(NSURL *)url {
    self = [super init];
    if (self) {
        _data = [NSMutableData data];
        _runLoopMode = CNSDataRunloopMode;
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
        [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
        _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
     
        while (!self.finished) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultMode beforeDate:[NSDate distantFuture]];
        }
    }
    return self;
}


你可能感兴趣的:(iOS-Bug: NSRunloop Mode)