Async NSURLConnection,Concurrent NSOperation,何时使用NSRunLoop? [英] Async NSURLConnection, Concurrent NSOperation, when to use NSRunLoop?

查看:182
本文介绍了Async NSURLConnection,Concurrent NSOperation,何时使用NSRunLoop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在次要线程(目标是iOS4)运行NSURLConnection异步,因为我已经创建了一个并发NSOperation,我想我几乎在那里,但不清楚以下:



1)在iOS4中NSOperationQueue addOperation在新线程中启动操作,因为使用GCD,基于技术问答QA1712 ,但我的测试(模拟器和iPad)显示start()总是在主线程上调用,任何想法,我需要在这里检查:如果在主线程然后产生一个新的?



2)如果start实际上通过addOperation() ,然后我可以通过调度当前的NSRunLoop启动我的异步NSURLConnection:

  [self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode :NSDefaultRunLoopMode]; 
[self.connection start];

像LinkedImageFetcher示例这里,我不需要循环,直到完成: / p>

  do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while(!isCompelted);假设我的自定义NSOperation start()在主线程上调用,并且2)是正确的,那么我们可以使用下面的代码:



<我在start()中创建了一个新线程来调用我的自定义main()方法:

  [NSThread detachNewThreadSelector:@selector (main)toTarget:self withObject:nil]; 

在我的main()我需要运行当前线程运行循环:

  do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while(!isCompelted);

这是唯一一个在并发NSOperation中管理NSURLConnection的实例,不确定我需要运行RunLoop,如果线程由GCD提供作为技术说明状态,我可以按照逻辑2)或者我还需要运行线程的runloop?

解决方案

如何测试由GCD提供的线程

好吧,我最终这样做,以便人们可能对细节感兴趣:



1)start()在主线程被调用,因为我正在使用[NSOperationQueue mainQueue],而不是使用[[NSOperationQueue allc] init]创建一个新队列。但是,在NSOperation start()中检查当前线程是否为mainThread,然后调用main()直接从那里或通过产生一个新线程(如果我们在mainThread上)没有受伤。



2)我意识到LinkedImageFetcher对于理解运行循环和线程太复杂,不必要的话,因为主题不那么复杂。 start()方法中所需要的是保持次线程运行循环,直到我们完成(isCompleted),所有的运行循环为NSURLConnection是监听来自连接和触发回调的输入(didreceivereponse, didreceivedata等)。



3)是运行循环是必要的,以获得连接回调在同一个(在这种情况下是次要的)线程。



有关详情,请参阅运行循环


I'm trying to run NSURLConnection async in a secondary thread (target is iOS4), for this I have created a concurrent NSOperation, I think I'm almost there, but am not clear on the following:

1) in iOS4 NSOperationQueue addOperation starts the operation in a new thread, because of the use of GCD, based on Technical Q&A QA1712, however, my tests (simulator and iPad) show that start() is always called on the main thread, any idea, do I need a check here: if on main thread then spawn a new one?

2) if start was actually called on a secondary thread by addOperation(), then I could start my async NSURLConnection by scheduling on the current NSRunLoop:

[self.connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.connection start];

like the LinkedImageFetcher example here and I would not need to loop until completed with:

do {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!isCompelted);

3) assuming my custom NSOperation start() is called on the main thread and 2) is correct, and I spawn a new thread in start() to call my custom main() method with:

[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];

In my main() I then do need to run the current thread run loop:

do {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!isCompelted);

This is the only instance I've managed to run NSURLConnection in a concurrent NSOperation, but I'm not sure I do need to run the RunLoop, if the thread was supplied by the GCD as the tech notes state, could I follow the logic in 2) or would I still have to run the thread's runloop? How can I test a thread supplied by GCD?

Many thanks for any elucidation

解决方案

Well, I eventually worked this out so thought people might be interested in the details:

1) start() was called on the main thread because I was using [NSOperationQueue mainQueue] rather than creating a new queue with [[NSOperationQueue allc] init]. However a check in NSOperation start() for whether the current thread was the mainThread or not and then calling main() directly from there or by spawning a new thread (in case we were on the mainThread) did not hurt.

2) I realized that LinkedImageFetcher is far too complex for an understanding of run loops and threads, and unnecessarily so, because the topic is not that complicated. All that is needed within the start() method is to keep the secondary thread run loop going until we're done (isCompleted), all the run loop does for a NSURLConnection is listening to the inputs from the connection and firing callbacks (didreceivereponse, didreceivedata, etc.).

3) yes running the run loop is necessary to get connection callbacks on that same (secondary in this case) thread.

For more see Run Loops in Threading Programming Guide.

这篇关于Async NSURLConnection,Concurrent NSOperation,何时使用NSRunLoop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆