NSURLConnection, tableview GUI hangs

I am showing the images in the tableview using NSData dataWithContentsOfURL method but when i scroll the tableview GUI gets hanged.so after searching over the forum i found that i can try with NSUrlConnection method. so i tried but i cant implement it successfully.

Please find my code below...

kindly help me out that how i can correctly done it..

// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DataIdentifier"] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:249.0/255.0 blue:230.0/255.0 alpha:2.0]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; profileName = [appDelegate.arrCommunityUserList objectAtIndex:indexPath.row]; NSString *imgName = [profileName.user_image stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSString *strValue = [NSString stringWithFormat:@"%d", profileName.userID]; if (tableView == myTableView) { cellRectangle = CGRectMake(15, 2, 75, 75 ); NSString *myurl = [NSString stringWithFormat: @"%@pics/photos/%@/%@",ConstantImgURL, strValue,imgName]; NSURL *url = [NSURL URLWithString: myurl]; imageView = [[UIImageView alloc] initWithFrame: cellRectangle]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:myurl]]; [NSURLConnection connectionWithRequest:request delegate:self]; // create the connection with the request // and start loading the data NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest: request delegate:self]; if (theConnection) { receivedData = [[NSMutableData data] retain]; } [cell.contentView addSubview:imageView]; } } return cell; } // did receive response - ( void )connection:( NSURLConnection * )connection didReceiveResponse:( NSURLResponse * )response //-------------------------------------------------------------------------------------------------- { NSLog(@"Received response: %@", response); } // get recieved data - ( void )connection:( NSURLConnection * )connection didReceiveData:( NSData * )data //---------------------------------------------------------------------------------- { // NSLog(@"Connection received data, retain count: %d", [connection retainCount]); [receivedData appendData:data]; } // finished loading - ( void )connectionDidFinishLoading:( NSURLConnection * )connection //------------------------------------------------------------------- { // Set appIcon and clear temporary data/image UIImage *image = [[UIImage alloc] initWithData:receivedData]; imageView.image = image; } // connection failed with error - ( void )connection:( NSURLConnection * )connection didFailWithError:( NSError * )connError //--------------------------------------------------------------------------------------- { // NSLog(@"Error receiving response: %@", connError); [connection release]; [receivedData release]; }

-------------Problems Reply------------

dataWithContentsOfURL is a synchronous network request. That means that where your code is called it will wait until the request finishes before moving on to the next instruction. Synchronous networking is bad. Really bad. It only really ever works in testing.

What you should be doing, is firing off asynchronous requests for these images. The reason why your code above is horrendously slow is that every single time that the tableView asks its dataSource delegate for cellForRowAtIndexPath:; your code fires off a network request synchronously - which means the cell won't be returned until the network request for the image is finished.

Instead, what you should be doing is either loading all the images asynchronously when the tableView is requested. Here's a good example which uses tags to identify them as they return. This is not easy in the whole context of what you're doing; so perhaps you might want to start all the NSURLConnections when the tableView is shown, return 0 for numberOfSectionsInTableView until the connections finish, then call reloadData on the tableView when they're all finished (and make numberOfSectionsInTableView now return the right number of rows to display).

Start using ASI library: http://allseeing-i.com/ASIHTTPRequest/How-to-use The sooner the better.

I think this might solve your Problem... http://www.markj.net/iphone-asynchronous-table-image/

Category:iphone Views:8 Time:2011-05-09
Tags: iphone ios4

Related post

  • Why Ruby 1.9 GUI hangs if i do any intensive computation in separate Ruby thread? 2012-01-30

    Ruby 1.9 is supposed to have native threads, and GIL is supposed to lift if some threads enters native code (like GUI toolkit main loop or C implementation of some Ruby lib). But if i start following simple code sample that displays GUI in main threa

  • iphone nsurlconnect, tableview and activity indicator 2010-11-25

    i've a method that perform a connection to retreive some data and popolate a tableview. This method works great. Now i'm launching this method in viewDidLoad with [NSThread detachNewThreadSelector:@selector(connTre) toTarget:self withObject:nil]; i'v

  • Windows Forms GUI hangs when calling OpenFileDialog.ShowDialog() 2011-07-16

    my project a three tier architecture project talking to a WCF service in the backend. When the backend is able to fetch data from the service, it notifies the business layer using publish-subscribe, which in return notifies the GUI layer. I have adde

  • How to show 'please wait' and avoid GUI hang ? 2011-12-18

    I wrote application that contain 2 activities. In the first one - i need to give the user the option to choose image from the gallery and i send this image to some server. The Server return some result - and if the result is OK i need to show the sec

  • Why does my GUI still hang even after using SwingUtilities.invokeLater? 2010-11-19

    I have this ActionListener that gets called in the EDT. My plot() function is computationally heavy, it can easily take five seconds. It made the GUI hang as expected. I added the SwingUtilities.invokeLater code and it still hangs. Shouldn't the GUI

  • Problems with Backgroundworker, GUI is hanged 2011-12-05

    I am using a background worker below an a form, and when i click a button it should generate a document, but the GUI hangs , i don't know why it does that, because i feel i m using the backgroundworker right.. can anybody helps in this ? private void

  • GUI Locking up when using PrintDialog and PrintPreviewDialog in .net 2008-12-05

    I'm using .net's PrintPreviewDialog and whenever it's generating a preview, it locks up my GUI in the background and makes it look like it has crashed until the preview is finished. Seeing how the .net's page progress window that pops up isn't a dial

  • Database access in GUI thread , bad isn't it? 2009-06-23

    I'm working through some MSDN examples, and some books on ADO.Net. What they all have in common is using the point/click/drag-drop design time feature in Visual Studio to develop database applications, binding data sets to controls etc. And the resul

  • NSURLConnection and bound pair of streams (CFStreamCreateBoundPair or CFStreamCreateBoundPairCompat) not working on iPhone 3G 2011-05-12

    I'm using SimpleURLConnections example to implement multipart/form-data POST request using bound pair of streams. It works great in Simulator and on iPhone 3GS/iPhone 4. When I try it on iPhone 3G (running 3.1.3) no data gets sent to the server. NSUR

  • GUI is non responsive while updating grid having 80 columns during heavy update 2011-06-23

    I am having an application which is for securities trading in stock exchange. It has real time market feed from one of the vendors. We are processing market data on worker thread(s) and while updating main GUI we are marshaling this data to GUI threa

  • Winforms should I multi thread or use event timer? 2009-03-05

    I currently have a thread that listens for data from the network and then runs rules on it. I then want to pass the data to the GUI. I am worried about having a deadlock in the GUI. I cant figure out were to put the mutexes on the GUI side. I am also

  • Display progress for big ListView add 2009-12-31

    brief history. I have a little reporting tool that I am creating that uses a backgroundWorker to do the reporting. Works great, the GUI updates nicely and reports what I want it to. However if I get a lot of results back and then try to update my Lis

  • PyQT: How do I handle loops in main? 2010-09-22

    I have an application that has a PyQT GUI. I set up the GUI in the usual PyQT fashion: app = QtGui.QApplication(sys.argv) win = MainWindow() win.initialize() win.show() sys.exit(app.exec_()) The problem is that right after this I have a while loop th

  • How to figure out who owns a worker thread that is still running when my app exits? 2010-12-21

    Not long after upgrading to VS2010, my application won't shut down cleanly. If I close the app and then hit pause in the IDE, I see this: The problem is, there's no context. The call stack just says [External code], which isn't too helpful. Here's wh

  • Scheduled tasks (Timers) in Swing 2010-12-23

    A GUI using Swing, must update part of its UI from a every 5 seconds. (output.setPage(url)) The code is running in a timer, but on every update, the GUI hangs. How do I avoid making the UI hang? Here is my code: <insert code here> -------------

  • Java Thread Management and Application Flow 2011-01-18

    I have a Java application that downloads information (Entities) from our server. I use a Download thread to download the data. The flow of the download process is as follows: Log in - The user entity is downloaded Based on the User Entity, download a

  • Can a context switch occur during a method call? 2011-04-05

    I am working on an application that interacts with a database via stored procedures. To prevent the GUI being unresponsive i run the stored procedures each in a different thread. I tested this with a loop in a thread and that works, but when i call a

  • UI Dispatch Buffering? 2011-04-28

    I have a conundrum. I have "inherited" a very badly designed and very complex system which I am modernizing and rebuilding (with my team) piece-by-piece. The problem is that the current system is depended on by 200+ users, and they are having major i

  • Continuous image loading/processing & display on button click (in Swing) 2011-06-28

    I have trouble with continuous image loading&processing&display in Swing: Below there is simple case where clicking a button causes my program to grab frame from webcam and then display this picture in jlabel. It works as it should, but i hav

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.177 (s). 11 q(s)