您的位置:首页 > 移动开发 > IOS开发

iOS-Bug: NSRunloop Mode

2014-04-10 14:12 495 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: