Detect if a dispatch_async method is still running - iphone

I have a loadImages method
- (void)loadImages {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//this method loads images from a url, processes them
//and then adds them to a property of its view controller
//#property (nonatomic, strong) NSMutableArray *storedImages;
});
}
When a button is clicked a view enters the screen, and all the images that currently exist in _storedImages are displayed
- (void)displayImages {
for (NSString *link in _storedImages) {
//displayImages
}
}
The problem with this setup is, that if the user clicks the button before all the images are loaded, not all the images are presented on the screen.
Hence, I would like to display an SVProgressHUD if the button is clicked, and the loadImages dispatch_async method is still running.
So, how do I keep track of when this dispatch_async is completed? Because if I know this, then I can display an SVProgressHUD until it is completed.
On a side note, if you know how to load/display the images dynamically that info would be helpful too, i.e. you click the button and then as you see the current images, more images are downloaded and displayed
Thank you from a first time iOS developer!
Ok I found a solution but it is incredibly inefficient, I am sure there's a better way to do this
1. Keep a boolean property doneLoadingImages which is set to NO
2. After the dispatch method finishes, set it to YES
3. In the display images method, have a while (self.doneLoadingImages == NO)
//display progress HUD until all the images a loaded

Keep in mind that NSMutableArray is not thread-safe. You must ensure that you don't try to access it from two threads at once.
Using a boolean to track whether you're still loading images is fine. Make loadImages look like this:
- (void)loadImages {
self.doneLoadingImages = NO;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
while (1) {
UIImage *image = [self bg_getNextImage];
if (!image)
break;
dispatch_async(dispatch_get_main_queue(), ^{
[self addImage:image];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
[self didFinishLoadingImages];
});
});
}
So we send ourselves addImage: on the main queue for each image. The addImage: method will only be called on the main thread, so it can safely access storedImages:
- (void)addImage:(UIImage *)image {
[self.storedImages addObject:image];
if (storedImagesViewIsVisible) {
[self updateStoredImagesViewWithImage:image];
}
}
We send ourselves didFinishLoadingImages when we've loaded all the images. Here, we can update the doneLoadingImages flag, and hide the progress HUD if necessary:
- (void)didFinishLoadingImages {
self.doneLoadingImages = YES;
if (storedImagesViewIsVisible) {
[self hideProgressHUD];
}
}
Your button action can then check the doneLoadingImages property:
- (IBAction)displayImagesButtonWasTapped:(id)sender {
if (!storedImagesViewIsVisible) {
[self showStoredImagesView];
if (!self.doneLoadingImages) {
[self showProgressHUD];
}
}
}

What I usually do with this kind of problem is basically the following (rough sketch):
- (void)downloadImages:(NSArray*)arrayOfImages{
if([arrayOfImages count] != 0)
{
NSString *urlForImage = [arrayOfImages objectAtIndex:0];
// Start downloading the image
// Image has been downloaded
arrayOfImages = [arrayOfImages removeObjectAtIndex:0];
// Ok let's get the next ones...
[self downloadImages:arrayOfImages];
}
else
{
// Download is complete, use your images..
}
}
You can pass the number of downloads that failed, or even a delegate that will receive the images after.

You put a "note" and this may help, it allows you to display the images as they come down, I store the URLs in an array (here it is strings but you could do array of NSURL).
for(NSString *urlString in imageURLs)
{
// Add a downloading image to the array of images
[storedImages addObject:[UIImage imageNamed:#"downloading.png"]];
// Make a note of the image location in the array
__block int imageLocation = [storedImages count] - 1;
// Setup the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setTimeoutInterval: 10.0];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// Check the result
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (data != nil && error == nil && [httpResponse statusCode] == 200)
{
storedImages[imageLocation] = [UIImage imageWithData:data];
[self reloadImages];
}
else
{
// There was an error
recommendedThumbs[imageLocation] = [UIImageimageNamed:#"noimage.png"];
[self reloadImages]
}
}];
}
You then need another method which reloads the display. If the images are in a table then [[tableview] reloaddata];
-(void)reloadImages

Related

iphone memory issue while downloading images

I have a wierd problem. The requirement is to download image from a url on swipe and display it in image view .It is working all fine but I am getting memory warnings after 30 images and after few more swipe app crashes.
Implementation is pretty straight forward,but already spent almost 2 days to figure out the issue.
On each swipe i am calling A method :-
-(void)callDownloadImageAPI{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
[self loadIndicator:#"Please Wait.!!" :#"Please be patient while we are downloading image for you"];
#try{
DownloadImage *downLoadImge =[[DownloadImage alloc] init];
downLoadImge.delegate=self;
[downLoadImge getImage:[self.allUrlArray objectAtIndex:self.initialImageViewCounter]];
}
#catch (NSException *e) {
NSLog(#"callDownloadImageAPI exception %#",[e description]);
[HUD hide:YES];
}
[pool release];
}
This method download 1 image at a time and send UIImage to its delegate
//Implementation of DownloadImage.h and .m
#protocol DownloadImageDelegate
#required
- (void)messageFormServerWithImage:(UIImage*)imageFromSever;
- (void)gettingImageFailed :(NSString*)errorDesc;
#end
#interface DownloadImage : NSObject
#property(strong) NSURLConnection* connection;
#property(weak) id<DownloadImageDelegate> delegate;
#property(strong) NSMutableData* data;
-(void)getImage:(NSString *)imageUrl;
//DownloadImage.m
-(void)getImage:(NSString *)imageUrl{
#autoreleasepool {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
#autoreleasepool {
NSLog(#"connectionDidFinishLoading");
self.connection=nil;
if( self.data == nil) {
return;
}
// NSString* jsonStr = [[NSString alloc] initWithData:self.data encoding:NSASCIIStringEncoding];
UIImage *img=[UIImage imageWithData:self.data];
// NSArray *messages_json = [parser objectWithString:jsonStr error:nil];
[self.delegate messageFormServerWithImage:img];
self.data = nil;
img= nil;
}
}
Other delegates of NSUrlConnections are implemented but I am not putting it here .
Once this image is returned i am setting this image to scrollview and displaying it and deleting the previous image from scrollview.
More Info:-
Just to verify i commented out setting image to scrollview and just downloaded images on each swipe but still it crashes around 30 images
Surprisingly I am using same class DownloadImage.h and .m to download image at other places in the same work and it work awesome even with 500images .
I am testing in iPod Touch and I checked the memory utilised remain between 12-14mb(never exceed this)
Please help me out guys,let me know if you need more detail.
Its crashing because all the images are being stored in virtual memory, you need to be caching them and then loading them back into memory when the user actually views them.
Try also setting your image objects to nil after they have been cached or are not needed.
In your class I would also recommend using the didReceiveMemoryWarning method and release some of your images from memory when this is called.

iPhone - Make UITableViewCells appear on view load before NSURLConnection completes

I am trying to create a loose version of LazyTabelImages using storyboard and JSON. in ViewDidLoad on my main TableViewController, I start an NSURLConnection to get the JSON data, but my cells do not load until after the connection is completed. I want the same behavior that LazyTableImages has, where the cells load as blanks, but then have the information filled in (reload the table data). I can duplicate this if I do not use storyboard, as LazyTables does not use storyboard, but that is not an option.
I have looked through LazyTableImages to try to find the solution, but storyboard make a big difference (to me anyway).
Is there a simple way to get the cells to load as blanks? For example, if the device has no internet, I still want my TableView to show up, and I will put a custom message in the cell.
Code:
The part of my viewDidLoad where I initialize the connection....
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
self.dataConnection = [[NSURLConnection alloc] initWithRequest:urlrequest delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
connectionDidFinnishLoading...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//ListData below is an array that my data received (JSON) is loaded into. It is then passed to getTableData.
self.dataConnection = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performSelectorOnMainThread:#selector(getTableData:) withObject:ListData waitUntilDone:YES];
});
}
getTableData...
-(void)getTableData:(NSData *)jsonData
{
NSError *error = nil;
arrayEntries = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];
for (int x = 0; x < arrayEntries.count; x++)
{
NSMutableDictionary *dic = [arrayEntries objectAtIndex:x];
//ARecord is a class just like in LazyTableImages that creates objects to keep the icons/data together. The ARecords are loaded into the TableView
ARecord *arecord = [[ARecord alloc] init];
NSString *title = [dic objectForKey:#"title"];
NSString *subt = [dic objectForKey:#"subtitle"];
NSString *url = [dic objectForKey:#"image_URL"];
arecord.Icon = nil;
arecord.URL = url;
arecord.Name = title;
arecord.title = subt;
//this is where I load an array full of the arecord objects.
[array addObject:arecord];
}
[self.tableView reloadData];
}
I've done something similar. In viewDidLoad: I set the array for table data to a few objects of [NSNull null] for however many blank rows I want to show while the data is downloading. In cellForRowAtIndexPath: I check if [self.arrayOfTableData objectAtIndex:indexPath.row] = [NSNull null]. If so return a "blank" cell, otherwise load the cell with ARRecrod data.
Then when the URL completes, replace the array of NSNulls with array of your ARRecords.
I do this with two objects. First, I have an image fetcher class that downloads data asynchronously and notifies a delegate when it's complete. Then I have an image view class that implements the fetcher's delegate methods. So something like:
#implementation AsyncImageFetcher
-(id)initWithURL:(NSURL *)aURL andDelegate:(id<SomeProtocol>)aDelegate{
//...
NSURLRequest *req = [NSURLRequest requestWithURL:aURL];
//Note that NSURLConnection retains its delegate until the connection
//terminates! See comments in MyImageView below.
[NSURLConnection connectionWithRequest:req delegate:self];
//...
}
//Implement standard connection delegates here. The important part is:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
// ...
UIImage *anImage = [self decodeDownloadedDataIntoAnImage];
if([[self delegate] respondsToSelector:#selector(imageFetcher:didFetchImage:)]){
[[self delegate] imageFetcher:self didFetchImage:anImage];
}
//...
}
#end
Then I subclass UIImageView or UIView or something (depending on how flexible you need to be) to implement the delegate protocol and fire off the fetcher:
#implementation MyImageView
-(id)initWithURL:(NSURL *)aURL andPlaceHolderImage:(UIImage *)aPlaceHolder{
//...
[self setImage:aPlaceHolder];
//Note we don't assign this to an ivar or retain it or anything.
//That's ok because the NSURLConnection inside the fetcher actually
//retains the fetcher itself. So it will live until the connection
//terminates -- which is exactly what we want. Thus we disable
//CLANG's noisy warnings.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
[[AsyncImageFetcher alloc] initWithURL:aURL andDelegate:self];
#pragma clang diagnostic pop
return self;
}
-(void)imageFetcher:(MCMAsyncImageFetcher *)anImageFetcher didFetchImage:(UIImage *)anImage{
[self setImage:anImage];
}
#end
In your specific case, you'd just set a MyImageView as your cell's imageView in -tableView:cellForRowAtIndexPath:, passing reasonable values for its placeholder and URL, of course.
Since I haven't see your code, I just give my suggestion here:
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
dispatch_async(queue, ^{
//add your connection code here
//parse the json and store the data
//
dispatch_async(dispatch_get_main_queue(), ^{
//here to reload your table view again,
//since UI related method should run on main thread.
[YOUR_TABLEVIEW reloadData];
});
});
[YOUR_TABLEVIEW reloadData];
}
Note: Make sure your tableview in storyboard has connected to that in code! Hope it helps!

How do you remove a UIButton/keyboard/UIAlertView from the view while json or other data is being loaded?

I'm using a UISearchBar in my application and the problem is when I call a few json methods searchBarSearchButtonClicked seems to not resign the keyboard until the other methods are done loading the data. I've tried alternatively using UIAlertView and UIButtons to replace the searchBarSearchButtonClicked function but they appear to literally freeze and stay in a "pressed down" state too. I was also wondering if this would be a reason why [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; wouldn't show an activity indicator in the device's status bar.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
self.args = searchBar.text;
[self grabData];
[self fillVars];
[searchBar resignFirstResponder];
[self.tableView reloadData];
}
[self grabData] is where I grab the JSON data and [self fillVars] just fills a few things that are later used.
-(void)grabData{
self.args = [self.args stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
urlString = [NSString stringWithFormat:#"%#%#?key=%#&q=%#",baseUrl,func,apiKey,args];
url = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
self.matches = [json objectForKey:#"matches"];
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
}
You will have to use threading. All manipulation with your interface happens on the main thread, so when you perform a lengthy task on the main thread, the interface won't be able to update itself before the task has completed.
In a UIViewController you can do [self performSelectorInBackground:#selector(grabData) withObject:self], which is a convenience method for dispatching a new queue (thread) using grand central dispact.
You could also do that manually, using the GCD API. You would do something along the lines of this:
dispatch_queue_t jsonQueue = dispatch_queue_create("JSON Queue", NULL);
dispatch_async(jsonQueue, ^{
// fetch JSON data ...
dispatch_async(dispatch_get_main_queue(), ^{
// perhaps do something back on the main queue once you're done!
});
});

Three20 TTURLRequest Synchronous Cache (make it async or thread it)?

I'm currently working with the Three20 library to implement some caching for an iPhone project. It has been a huge help for speeding up my table views and page loads, however I'm running into a small issue:
I have subclassed TTURLImageResponse to resize/crop images that are retrieved remotely before caching them so that I don't have to resize them each time I get them back from the server/cache. This works very well and the images load quite quickly, however when images are returned from the cache in the Three20 library they are returned synchronously. This results in a noticeable delay when firing off 10-20 image requests that come back from the cache. Since they are coming back synchronously, they are blocking my UI thread and the user is left waiting for a couple seconds before they see the images that came back from the cache.
I have attempted to thread the operation by doing the following:
- (void) setupImages
{
if(imageList != nil &&
[imageList count] > 0)
{
[NSThread detachNewThreadSelector:#selector(fillImages) toTarget:self withObject:nil];
}
else
{
lblMessage.hidden = NO;
lblMessage.text = #"There are no images. Try refreshing the current list.";
lblTitle.text = #"";
lblAuthor.text = #"";
}
}
- (void)fillImages
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i=0;
TTURLRequest *request;
for (MyImage *imageObj in imageList)
{
NSString *imageUrl = [[[imageObj thumbnail_lg] image_url] absoluteString];
if(imageUrl != nil)
{
request = [TTURLRequest requestWithURL:imageUrl delegate:self];
MyTTURLImageResponse *responseObj = [[[MyTTURLImageResponse alloc] init] autorelease];
responseObj.index = i;
request.cachePolicy = TTURLRequestCachePolicyDisk;
request.response = responseObj;
request.httpMethod = #"GET";
[request send];
i++;
}
}
[pool release];
}
//When the fillImages selector is spawned on its own thread it does not callback to this method, therefore images are never loaded!!
- (void)requestDidFinishLoad:(TTURLRequest*)request
{
MyTTURLImageResponse *response = request.response;
UIImage *loadedImage = response.image;
int imageIndex = response.index;
//this now happens as the image is cached :)
//loadedImage = [loadedImage cropCenterAndScaleImageToSize:CGSizeMake(200, 200)];
[myImageDisplay setImage:loadedImage forIndex:imageIndex];
}
However the threading doesn't appear to be working (the callback is on the main thread...). Does anyone know of a way to retrieve from the Three20 Cache either ASYNCHRONOUSLY or a way of threading the Three20 TTURLRequest so that it's at least doing this in the background?

Static variable for communication among like-typed objects

I have a method that asynchronously downloads images. If the images are related to an array of objects (a common use-case in the app I'm building), I want to cache them. The idea is, I pass in an index number (based on the indexPath.row of the table I'm making by way through), and I stash the image in a static NSMutableArray, keyed on the row of the table I'm dealing with.
Thusly:
#implementation ImageDownloader
...
#synthesize cacheIndex;
static NSMutableArray *imageCache;
-(void)startDownloadWithImageView:(UIImageView *)imageView andImageURL:(NSURL *)url withCacheIndex:(NSInteger)index
{
self.theImageView = imageView;
self.cacheIndex = index;
NSLog(#"Called to download %# for imageview %#", url, self.theImageView);
if ([imageCache objectAtIndex:index]) {
NSLog(#"We have this image cached--using that instead");
self.theImageView.image = [imageCache objectAtIndex:index];
return;
}
self.activeDownload = [NSMutableData data];
NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
self.imageConnection = conn;
[conn release];
}
//build up the incoming data in self.activeDownload with calls to didReceiveData...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Finished downloading.");
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
self.theImageView.image = image;
NSLog(#"Caching %# for %d", self.theImageView.image, self.cacheIndex);
[imageCache insertObject:image atIndex:self.cacheIndex];
NSLog(#"Cache now has %d items", [imageCache count]);
[image release];
}
My index is getting through okay, I can see that by my NSLog output. But even after my insertObject: atIndex: call, [imageCache count] never leaves zero.
This is my first foray into static variables, so I presume I'm doing something wrong.
(The above code is heavily pruned to show only the main thing of what's going on, so bear that in mind as you look at it.)
You seem to never initialize the imageCache and probably got lucky with it having the value 0. The initialization would best be done in the class' initialization, e.g.:
#implementation ImageDownloader
// ...
+(void)initialize {
imageCache = [[NSMutableArray alloc] init];
}
// ...