简体   繁体   中英

do we need to perform main() of nsoperation in main thread , nsoperation

I am working on NSOperation in iOS and getting lost totally. I go over some documentations and notice that for start method, some sample codes execute it in main thread and some execute in back ground thread. So I go straight and give it a try based on a given sample codes. Below is UrlDownloadOperation.m ( subclass of 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];

} 

In order to execute the operation, I am doing :

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];
}

The log shows me that which one starts and finish and how many operations left in a queue.Eventually, the total is 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 I take out

if (![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
        return;
    }

All operations start but I don't get any responses from their delegate at all and now my queue is still containing 3 operations.

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

My questions are

  1. why we have to execute start in a main thread. I think we should not because we are downloading data and it should happen in back ground thread.

  2. Why and when we decide to do isCurrent return YES like above.

Sorry for the long post. I hope i can understand my problems with your helps. Thanks in advanced

why we have to execute start in a main thread. I think we should not because we are downloading data and it should happen in back ground thread.

The start method takes pains to run on the main thread so that it schedules itself on the main run loop, which ensures that the delegate will be called. When used this way, though, NSURLConnection 's work all happens asynchronously , so you don't have to worry about blocking the main thread.

Why and when we decide to do isCurrent return YES like above.

isConcurrent is a method inherited from NSOperation that you need to override if your operation runs concurrently. From the NSOperation reference page:

If you are implementing a concurrent operation, you must override this method and return YES from your implementation. For more information about the differences between concurrent and non-concurrent operations, see “Concurrent Versus Non-Concurrent Operations.”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM