2014-09-26 37 views
0

我在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 

我的问题是

  1. 为什么我们在主线程中执行start。我认为我们不应该因为我们正在下载数据而应该发生在后台线程中。

  2. 为什么当我们决定做isCurrent返回YES像上面那样。

对不起,很长的文章。我希望我能用你的帮助理解我的问题。先进的感谢

回答

0

为什么我们必须在主线程中执行启动。我认为我们不应该因为我们正在下载数据而应该发生在后台线程中。

start方法需要努力在主线程上运行,以便它在主运行循环上调度自己,以确保委托将被调用。当使用这种方式时,虽然NSURLConnection的工作都发生在asynchronously,所以你不必担心阻塞主线程。

为什么当我们决定做isCurrent返回YES就像上面一样。

isConcurrent是一种继承自NSOperation的方法,如果您的操作并发运行,则需要覆盖该方法。从NSOperation参考页:

如果要实现并行操作,必须重写此 方法,并从您的实现返回YES。有关并发和非并发 操作之间差异的更多信息,请参阅“并发与非并发操作”。“

+0

感谢您的评论,但我仍然迷失在第一个问题(启动方法)。如果你可以给我一些参考,它也是有帮助的。 – tranvutuan 2014-09-26 20:06:53

相关问题