Tag: nsrunloop

iOS,NSURLConnection:委托不同的线程callback?

我怎样才能让NSURLConnection调用它的委托方法从一个不同的线程,而不是主线程。 我试图搞乱scheduleInRunLoop:forMode:但似乎并没有做我想要的。 我必须下载一个大文件,并频繁地中断主线程,以至于一些正在发生的渲染开始变得波涛汹涌。 NSURLRequest * request = [NSURLRequest requestWithURL:url]; NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; NSRunLoop * loop = [NSRunLoop currentRunLoop]; NSLog(@"loop mode: %@",[loop currentMode]); [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; [connection start]; 另外一件我没有看到的东西是“模式”只有两种模式被logging下来,所以没有太多真正的testing。 有任何想法吗? 谢谢

正在调用 – 一个好主意?

是否通常是一个好主意-[NSRunLoop runUntilDate:] ? 它看起来没有任何问题,但是让我感到紧张的是告诉运行循环从运行循环内运行。 更多信息: 我现在有一个项目正在从REST服务中获取数据。 需要获得的关键信息之一是具有有效数据的date范围。 这是一小部分只需要一次获取的数据,所以我决定处理它的最好方法是让属性下载数据,如果局部variablesnil 。 我正在使用ASIHTTPRequest和一个ASINetworkQueue ,所以一切都是asynchronous默认情况下,为了这个工作,这个属性不能返回,直到数据已被下载和处理。 下面是我的代码的大纲,variables的名称已经改变,以保护无辜: __block BOOL isWorking = YES; __block ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:/*actual URL*/] autorelease]; [request setCompletionBlock:^{ // set local variable isWorking = NO; }]; [request setFailedBlock:^{ // show alert to user isWorking = NO; }]; [queue addOperation:request]; while (isWorking) { [[NSRunLoop currentRunLoop] runUntilDate:[NSDate […]

iOS上的runloop中的操作顺序

iOS上的操作顺序是什么? 我正在考虑关于时机 setNeedsLayout和layoutSubviews setNeedsDisplay和drawRect 触摸识别 [NSTimer scheduledTimerWithTimeInterval:0.000001 tar(…)] dispatch_async(dispatch_get_main_queue(), ^{ /* code */} 作为一个答案的例子,我想收到它可能是这样的格式: main上的dispatch_async在下一个运行周期之前发生 drawRect在运行周期结束时发生

用GCD运行重复NSTimer?

我想知道为什么当你在GCD块中创build一个重复定时器时,它不工作? 这工作正常: -(void)viewDidLoad{ [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; } -(void)runTimer{ NSLog(@"hi"); } 但是这个工作: dispatch_queue_t myQueue; -(void)viewDidLoad{ [super viewDidLoad]; myQueue = dispatch_queue_create("someDescription", NULL); dispatch_async(myQueue, ^{ [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; }); } -(void)runTimer{ NSLog(@"hi"); }

了解NSRunLoop

任何人都可以解释什么是NSRunLoop ? 所以我知道NSRunLoop是一个与NSThread连接的东西吗? 所以假设我创build了一个线程 NSThread* th=[[NSThread alloc] initWithTarget:self selector:@selector(someMethod) object:nil]; [th start]; -(void) someMethod { NSLog(@"operation"); } 所以这个线程完成后,他的工作权利? 为什么使用RunLoops或在哪里使用? 从苹果文档我已经看了一些东西,但不清楚,所以请尽可能简单解释