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
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.Why and when we decide to do
isCurrent
returnYES
like above.
Sorry for the long post. I hope i can understand my problems with your helps. Thanks in advanced