views:

52

answers:

2

I have a custom UITableViewCell with, among other things, a label and an imageView. If the table view is the visible view and it has a cell on it, if I programmatically update the label's text and the imageView's image and log when I do it, it takes about 4 seconds AFTER I see the notification in the logs for the label and image to update. Here is the order of what happens and how:

-Load the view containing the table and one custom cell

-Write a file to disk using and NSOperation (and queue)

-When the NSOperation is finished writing the file it fires a NSNotification (defaultCenter) which tells the cell's view to update the label and image AND I set [self setNeedsDisplay]

-Here I see in my logs that the cell has gotten the notification AND finished setting the new values

-~4 seconds later the cell's view actually updates.

What gives?

A: 

You should call reloadRowsAtIndexPaths:withAnimation: to update a particular cell in the table view.

progrmr
That function would work but its a heavy handed solution since modifying the view should cause it to update, reloadRowsAtIndexPaths:withAnimation: actually asks it to reload the cell. The solution involved the NSOperation queue I mentioned, answer below.
Shizam
@shizam: Thanks for explaining. I'm still learning how the threading and event loops interact in iOS. It ought to be documented somewhere.
progrmr
@progrmr Certainly, yea I went back and reread their sample NSOperationSample.xcodeproj project again to step through how they suggest using it and saw them using performSelectorOnMainThread.
Shizam
+2  A: 

The solution to my particular problem hinged on the way in which I'm updating the data, I'm using an NSOperation (and queue) to send a notification, the notification responder then updates the view. Problem is an NSOperation technically lives on another thread so what I should have done is this:

-Write a file to disk using and NSOperation (and queue)

-When the NSOperation is finished writing the file it fires a NSNotification (defaultCenter), the responder should then performSelectorOnMainThread a method that does the actual updating of the view

Did this and my cell updates right away.

Shizam