Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've just figured out where my problem is, but not really understand why.</p> <p>In my <code>ImageDownloader</code> class, I set up a runloop with <code>done</code> and <code>currentRunLoop</code> variables. In the main method, I have a while loop for forcing the <code>currentRunLoop</code> run.</p> <p>As I remove those "runLoop" stuffs, the app runs smoothly on both iOS6 and iOS5.</p> <p>So change the entire <code>ImageDownloader.m</code> with these lines then it works (I commented out some useless (say harmful) lines):</p> <pre><code>// // ImageLoader.m // DownloadImagesTableView // // Created by Viet Ta Quoc on 6/25/13. // Copyright (c) 2013 Viet Ta Quoc. All rights reserved. // #import "ImageDownloader.h" @implementation ImageDownloader @synthesize downloadData,delegate,queue,done,customTag; NSRunLoop *currentRunLoop; -(void)downloadImageWithURL:(NSString *)imageUrl queue:(NSOperationQueue*)opQueue andTag:(int)tag{ self.customTag= tag; self.queue = opQueue; // self.done = NO; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; [connection start]; // currentRunLoop = [NSRunLoop currentRunLoop]; NSLog(@"Start downloading image %d...",customTag); } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"Received response..."); downloadData=[[NSMutableData alloc] initWithLength:0]; expectedDataLength=[response expectedContentLength]; NSLog(@"Image %d size: %lld kb",customTag,[response expectedContentLength]/1024); } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ float receivedLenght = [data length]; receivedDataLength=(receivedDataLength+receivedLenght); float progress=(float)receivedDataLength/(float)expectedDataLength; [delegate updateProgess:progress andIndex:[NSIndexPath indexPathForRow:customTag-100 inSection:0]]; [self.downloadData appendData:data]; // NSLog(@"Percentage of data received of tag %d: %f %%",self.customTag,progress*100); } -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ [delegate finishedDownloadingImage:downloadData andTag:customTag]; // done = YES; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Network Connection Failed?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; // NSLog(@"%@",[error debugDescription]); NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); [alert show]; } -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ NSLog(@"Got here *(*&amp;(**&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;(*&amp;"); } -(void)main{ // do{ //// NSLog(@"Running....1"); // [currentRunLoop runUntilDate:[NSDate distantFuture]]; // // [currentRunLoop run]; // } while (!done); // [currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; } @end </code></pre> <p>Thank you guys for your supports.</p> <p><strong>==================================================================================</strong></p> <p><strong>P/s: for anyone who interested in this problem, I update here my entire solution: <a href="https://dl.dropboxusercontent.com/u/15628608/Public%20files/DownloadImageByOperation_new.zip" rel="nofollow">DownloadImage_Final</a></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI just took a look at your project. There are a couple of things: 1. you are not correctly inheriting from NSOperation. 2. You are not using an NSOperation correctly. 3. You better encapsulate the run loop into the NSOperation subclass (use either the main thread or a dedicated thread with a run loop). 4. A few design issues. So basically, the code won't work. I would suggest, read the docs about NSOperation and how to subclass correctly. As a jump start look at `QHTTPOperation` in Apple's samples (MVCNetworking).
      singulars
    2. COAnother jump start: on Gist there is a simplistic sample which defines a class `SimpleGetHTTPRequest` encapsulating a `NSURLConnection` and related state info. It can be easily modified to be subclassed from `NSOperation`. It has been specifically designed to give users an idea how to create your own "HTTPRequestOperation" class and has been steadily improved. Have fun ;) [SimpleGetHTTPRequest](https://gist.github.com/couchdeveloper/5764723)
      singulars
    3. COAnother issue in your code: in `addDownloadProcess:` you dispatch onto _dispatchQueue_ (which is presumable not the main queue) where the block invokes `downloadImageWithURL:`. The latter method then schedules the delegates on the current thread's run loop. However, the current thread of the dispatch queue is _unspecified_ and likely has no run loop. That has the effect, that your delegates will not be called. In order to fix this, use a dedicated thread with a run loop or use the main thread _explicitly_. Preferable implement this into your "HTTPRequest" class.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload