我在iOS的NSOperation上工作并且完全丢失。我查阅了一些文档,注意到对于start
方法,一些示例代码在主线程中执行它并且一些在后台线程中执行。所以我直接试着根据给定的示例代码尝试一下。下面是UrlDownloadOperation.m(的NSOperation的子类):我们是否需要在主线程中执行nsoperation的main(),nsoperation
- (BOOL)isConcurrent {
return YES;
}
-(void)main {
}
- (void)start
{
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}
NSLog(@"opeartion for <%@> started in mainThread = %d.", _url,[NSThread isMainThread]);
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
NSURLRequest * request = [NSURLRequest requestWithURL:_url];
_connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (_connection == nil)
[self finish];
}
为了执行该操作,我做:
NSArray * urls = [NSArray arrayWithObjects:@"http://people.csail.mit.edu/gremio/logos/google.jpg",
@"http://www.google.com/",
@"http://www.apple.com/",
nil];
for (NSString * url in urls) {
UrlDownloaderOperation * operation =
[UrlDownloaderOperation urlDownloaderWithUrlString:url];
[_queue addOperation:operation];
}
日志显示我,其中一个开始和完成,有多少操作留在queue.Eventually,总为0
2014-09-26 13:06:13.369 Concurrent[37401:303] Queue size: 1
2014-09-26 13:06:13.370 Concurrent[37401:303] Queue size: 2
2014-09-26 13:06:13.370 Concurrent[37401:303] Queue size: 3
2014-09-26 13:06:13.370 Concurrent[37401:303] opeartion for <http://people.csail.mit.edu/gremio/logos/google.jpg> started in mainThread = 1.
2014-09-26 13:06:13.370 Concurrent[37401:303] opeartion for <http://www.google.com/> started in mainThread = 1.
2014-09-26 13:06:13.370 Concurrent[37401:303] opeartion for <http://www.apple.com/> started in mainThread = 1.
2014-09-26 13:06:13.371 Concurrent[37401:303] operation for <http://people.csail.mit.edu/gremio/logos/google.jpg> finished. status code: 200, error: (null), data size: 283114
2014-09-26 13:06:13.371 Concurrent[37401:303] Queue size: 2
2014-09-26 13:06:13.372 Concurrent[37401:303] operation for <http://www.apple.com/> finished. status code: 200, error: (null), data size: 9380
2014-09-26 13:06:13.372 Concurrent[37401:303] Queue size: 1
2014-09-26 13:06:13.600 Concurrent[37401:303] operation for <http://www.google.com/> finished. status code: 200, error: (null), data size: 9970
2014-09-26 13:06:13.601 Concurrent[37401:303] Queue size: 0
如果我拿出
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}
所有的操作都开始了,但是我根本没有收到任何来自他们委托的响应,现在我的队列仍然包含3个操作。
2014-09-26 13:11:55.806 Concurrent[38365:303] Queue size: 1
2014-09-26 13:11:55.806 Concurrent[38365:303] Queue size: 2
2014-09-26 13:11:55.806 Concurrent[38365:3123] opeartion for <http://people.csail.mit.edu/gremio/logos/google.jpg> started
2014-09-26 13:11:55.807 Concurrent[38365:2007] opeartion for <http://www.google.com/> started
2014-09-26 13:11:55.807 Concurrent[38365:303] Queue size: 3
2014-09-26 13:11:55.807 Concurrent[38365:1003] opeartion for <http://www.apple.com/> started
我的问题是
为什么我们在主线程中执行
start
。我认为我们不应该因为我们正在下载数据而应该发生在后台线程中。为什么当我们决定做
isCurrent
返回YES
像上面那样。
对不起,很长的文章。我希望我能用你的帮助理解我的问题。先进的感谢
感谢您的评论,但我仍然迷失在第一个问题(启动方法)。如果你可以给我一些参考,它也是有帮助的。 – tranvutuan 2014-09-26 20:06:53