ASIHttpRequest problems. "unrecognized selector sent to instance" - iphone

I am experiencing problems using ASIHttpRequst. This is the error I get:
2010-04-11 20:47:08.176 citybikesPlus[5885:207] *** -[CALayer rackDone:]: unrecognized selector sent to instance 0x464a890
2010-04-11 20:47:08.176 citybikesPlus[5885:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[CALayer rackDone:]: unrecognized selector sent to instance 0x464a890'
2010-04-11 20:47:08.176 citybikesPlus[5885:207] Stack: (
33936475,
2546353417,
34318395,
33887862,
33740482,
126399,
445238,
33720545,
33717320,
40085013,
40085210,
3108783,
11168,
11022
)
And this is my code (Part of it):
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[image setImage:[UIImage imageNamed:#"bullet_rack.png"]];
BikeAnnotation *bike = [[annotationView annotation] retain];
bike._sub = #"";
[super viewDidLoad];
NSString *newUrl = [[NSString alloc] initWithFormat:rackUrl, bike._id];
NSString *fetchUrl = [newUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[networkQueue cancelAllOperations];
[networkQueue setRequestDidFinishSelector:#selector(rackDone:)];
[networkQueue setRequestDidFailSelector:#selector(processFailed:)];
[networkQueue setDelegate:self];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:fetchUrl]] retain];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[networkQueue addOperation:request];
[networkQueue go];
}
- (void)rackDone:(ASIHTTPRequest *)request
{
NSString *resultSearch = [request responseString];
NSData *data = [resultSearch dataUsingEncoding:NSUTF8StringEncoding];
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary * dict = (NSDictionary*)[NSPropertyListSerialization
propertyListFromData:data
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
rackXmlResult* fileResult = [[[rackXmlResult alloc] initWithDictionary:dict] autorelease];
rackXmlSet *rackSet = [fileResult getRackResult];
NSString *subString = [[NSString alloc] initWithFormat:#"Cyklar tillgÃĪngligt: %# -- Lediga platser: %#", rackSet._ready_bikes, rackSet._empty_locks];
[activity setHidden:YES];
[image setHidden:NO];
BikeAnnotation *bike = [annotationView annotation];
bike._sub = subString;
}
- (void) processFailed:(ASIHTTPRequest *)request
{
UIAlertView *errorView;
NSError *error = [request error];
NSString *errorString = [error localizedDescription];
errorView = [[UIAlertView alloc]
initWithTitle: NSLocalizedString(#"Network error", #"Network error")
message: errorString
delegate: self
cancelButtonTitle: NSLocalizedString(#"Close", #"Network error") otherButtonTitles: nil];
[errorView show];
[errorView autorelease];
}
The process is loaded as LeftCalloutView in the callout bubble when annotations are loaded in my mapview, so quite a lot (80 times or so).
It is meant to retrieve a XML Plist from a server, parse it and use the data... but it dies at the rackDone:
Does anybody have any ideas?
Regards,
Paul Peelen

Well, the problem appears to be that your networkQueue object (whatever that is NSOperationsQueue subclass?) is sending the rackDone: message to the viewController's view instead of the viewController itself. CALayers are attributes of views not viewControllers so the error has to be coming from a view.
Check the code for networkQueue.
You also don't seem to be using the self notation for networkQueue and if its a property of the class it may die without warning because it is not properly retained.

I found the solution using [self retain]. It seems that because I load the object so often, it doesn't allocate it self long enough to receive the rackDone: action.
It works now. Now I just need to figure out how I can load this on request, not when the app is loaded.

Related

UITabelView doesn't get updated with new content from server side

I have an app which has an UITabelView which gets its content from a server side.In my app i read the content from the server each 30 seconds....for that I use a NSTimer.
This NSTimer gets initialized when I load the view which contains the UITableView and it is invalidated when I leave this view.
My problem is this:
if on the server side the content for the UITableView is updated with new item and the JSON received in the iphone app as a response to a request to the server contains that item....the UITableView that appears on the screen is still not updated.
Here is how I did it:
//the timer is started when this view is loaded and the method repeatServerRequest is called
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(playlistTimer == nil)
playlistTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target: self selector: #selector(repeatServerRequest) userInfo: nil repeats: YES];
}
//the method repeatServerRequest starts a new thread in the background which does a request //to server for downloadeding the content
- (void) repeatServerRequest{
[NSThread detachNewThreadSelector:#selector(backgroundThinking) toTarget:self withObject:nil];
}
- (void) backgroundThinking{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:#"a link to server"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[pool release];
}
///when the response from server comes in these methods are called:
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self performSelectorOnMainThread:#selector(didFindAnswer:) withObject:request waitUntilDone:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"the value of error %#", error);
}
- (void) didFindAnswer:(ASIHTTPRequest *) request{
NSLog(#"update tabel");
SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];
NSArray *statuses = [parser objectWithString:responseString error:nil];
streams = [statuses valueForKey:#"_playLists"];
[parser release];
playList = [[NSMutableArray alloc] init];
idList = [[NSMutableArray alloc] init];
int ndx;
for (ndx = 0; ndx<streams.count; ndx++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:ndx];
[playList addObject:[stream valueForKey:#"name"]];
[idList addObject:[stream valueForKey:#"id_playlist"]];
}
NSLog(#"playList %#", playList);
oneview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
tableViewPlaylist =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableViewPlaylist.bounces=NO;
tableViewPlaylist.backgroundColor=[UIColor clearColor];
[tableViewPlaylist setDelegate:self];
[tableViewPlaylist setDataSource:self];
}
So when I update the content on server side, the JSON thet I get as a response on server side is updated but the UITAbelView is not, UNLESS I RUN AGAIN MY APP.Any idea why?
Two main problems:
You are creating a new UITableView every time the server updates. This is unnecessary.
Even if you wanted a new tableView, you are not adding it as a subview of the main view.
What you should do instead is don't create a new tableView at all. After you've updated the data model call [tableView reloadData] on the main thread. Your table will update and all is good. Assuming you have everything configured correctly and it sounds like you do if you see the expected data when you launch the app.

request send get error: [CFString release]: message sent to deallocated instance 0x6a83e00

I want to check the app version from apple so I send request like below
- (void)connectToCheckVersion{
NSString *url = #"http://itunes.apple.com/lookup?id=466424846";
TTURLRequest *_request = [TTURLRequest requestWithURL:url delegate:self];
_request.httpMethod = #"GET";
_request.cachePolicy = TTURLRequestCachePolicyNone;
_request.shouldHandleCookies = NO;
TTURLJSONResponse* response = [[TTURLJSONResponse alloc] init];
_request.response = response;
TT_RELEASE_SAFELY(response);
[_request send];
}
- (void)requestDidFinishLoad:(TTURLRequest*)request {
TTURLJSONResponse* response = request.response;
NSDictionary* json = response.rootObject;
NSArray *results = [json objectForKey:#"results"];
NSString *version;
for (NSDictionary *rawResult in results) {
version = [rawResult objectForKey:#"version"];
}
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
if (version != nil && currentVersion != nil && ![version isEqualToString:currentVersion]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"info"
message:#"newer version"
delegate:self
cancelButtonTitle:#"ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
and after [_request send]; will get [CFString release]: message sent to deallocated instance 0x6a83e00. I checked all Strings in this method seems they are ok, and I can still get correct response from remote.
If I comment out this connectToCheckVersion method then no any problem.
Any diea?
I think that you should retain the _request variable and save it as a member.
Because it will autorelease after the function is returned.
You have to release it after the request is successed or failed.
Thank you.

memory leak with NSMutableData

I have a class for connecting with httprequests. I am getting a memory leak for "NSMutableData" altho I am releasing it in "didFailWithError" and in "connectionDidFinishLoading" of the connection object:
- (BOOL)startRequestForURL:(NSURL*)url {
[url retain];
NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
// cache & policy stuff here
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPShouldHandleCookies:YES];
NSURLConnection* connectionResponse = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
if (!connectionResponse)
{
// handle error
return NO;
} else {
receivedData = [[NSMutableData data] retain]; // memory leak here!!!
}
[url release];
[urlRequest release];
return YES;}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
UIAlertView *alert =
[[[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Connection problem", nil)
message:NSLocalizedString(#"A connection problem detected. Please check your internet connection and try again.",nil)
delegate:self
cancelButtonTitle:NSLocalizedString(#"OK", nil)
otherButtonTitles:nil, nil]
autorelease];
[alert show];
[connectionDelegate performSelector:failedAction withObject:error];
[receivedData release];}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[connectionDelegate performSelector:succeededAction withObject:receivedData];
[receivedData release];}
The static analyser will call this a leak because you are not guaranteeing that either of the methods featuring a release will actually be called.
If you set receivedData as a retained property, and do
self.receivedData = [NSMutableData data];
Then in your dealloc (and also your didFail and didFinish, instead of the release):
self.receivedData = nil;
You will be OK.
As jbat100 points out, you are also leaking url and urlRequest if the !connectionResponse, unless you have omitted this code from the question
You need to make really sure that these two delegate methods are the only possible way the request could finish. I can see a leak here
if (!connectionResponse)
{
// handle error
return NO;
}
you do not do the release operations
[url release];
[urlRequest release];
Which you do when the connectionResponse is non-nil. On another note I strongly suggest the ASIHTTP Obj C library for doing this type of stuff.
if you want remove this leak take NSURLConnection in .h file and release that in connectionDidFinishLoading method .reason is you are allocted NSURLConnection object there but you cann't release over there if release app kill over there .that why you have to create NSURLConnection object in .h
Why do you think you are leaking? (NSMutableData) If it is because of Xcode's Analyze option; well, it lies, as it can't handle even such obvious complex situations.
However, as Narayana pointed out, you are also leaking the connection, which you should release in both the finish and fail delegate methods.

NSOperation and EXC_BAD_ACCESS

I have a few apps which are largely data driven, so most screens are basically composed of:
Open the screen
Download the data via an NSOperation
Display data in a UITableView
Make a selection from the UITableView
Go to new screen, and start over from step 1
I am finding that everything works in normal usage, but if the user goes away from the app for a while and then comes back, I'm getting an EXC_BAD_ACCESS error when the next NSOperation runs. This doesn't seem to matter if the user sends the app into the background or not, and it only seems to occur if there's been at least a few mins since the previous data connection was made.
I realise this must be some form of over-releasing, but I'm pretty good with my memory management and I can't see anything wrong. My data calls generally look like this:
-(void)viewDidLoad {
[super viewDidLoad];
NSOperationQueue* tmpQueue = [[NSOperationQueue alloc] init];
self.queue = tmpQueue;
[tmpQueue release];
}
-(void)loadHistory {
GetHistoryOperation* operation = [[GetHistoryOperation alloc] init];
[operation addObserver:self forKeyPath:#"isFinished" options:0 context:NULL];
[self.queue addOperation:operation];
[operation release];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:#"isFinished"] && [object isKindOfClass:[GetHistoryOperation class]]) {
GetHistoryOperation* operation = (GetHistoryOperation*)object;
if(operation.success) {
[self performSelectorOnMainThread:#selector(loadHistorySuceeded:) withObject:operation waitUntilDone:YES];
} else {
[self performSelectorOnMainThread:#selector(loadHistoryFailed:) withObject:operation waitUntilDone:YES];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
-(void)loadHistorySuceeded:(GetHistoryOperation*)operation {
if([operation.historyItems count] > 0) {
//display data here
} else {
//display no data alert
}
}
-(void)loadHistoryFailed:(GetHistoryOperation*)operation {
//show failure alert
}
And my operations generally looks something like this:
-(void)main {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError* error = nil;
NSString* postData = [self postData];
NSDictionary *dictionary = [RequestHelper performPostRequest:kGetUserWalkHistoryUrl:postData:&error];
if(dictionary) {
NSNumber* isValid = [dictionary objectForKey:#"IsValid"];
if([isValid boolValue]) {
NSMutableArray* tmpDays = [[NSMutableArray alloc] init];
NSMutableDictionary* tmpWalksDictionary = [[NSMutableDictionary alloc] init];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSArray* walksArray = [dictionary objectForKey:#"WalkHistories"];
for(NSDictionary* walkDictionary in walksArray) {
Walk* walk = [[Walk alloc] init];
walk.name = [walkDictionary objectForKey:#"WalkName"];
NSNumber* seconds = [walkDictionary objectForKey:#"TimeTaken"];
walk.seconds = [seconds longLongValue];
NSString* dateStart = [walkDictionary objectForKey:#"DateStart"];
NSString* dateEnd = [walkDictionary objectForKey:#"DateEnd"];
walk.startDate = [JSONHelper convertJSONDate:dateStart];
walk.endDate = [JSONHelper convertJSONDate:dateEnd];
NSString* dayKey = [dateFormatter stringFromDate:walk.startDate];
NSMutableArray* dayWalks = [tmpWalksDictionary objectForKey:dayKey];
if(!dayWalks) {
[tmpDays addObject:dayKey];
NSMutableArray* dayArray = [[NSMutableArray alloc] init];
[tmpWalksDictionary setObject:dayArray forKey:dayKey];
[dayArray release];
dayWalks = [tmpWalksDictionary objectForKey:dayKey];
}
[dayWalks addObject:walk];
[walk release];
}
for(NSString* dayKey in tmpDays) {
NSMutableArray* dayArray = [tmpWalksDictionary objectForKey:dayKey];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"startDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray* sortedDayArray = [dayArray sortedArrayUsingDescriptors:sortDescriptors];
[sortDescriptor release];
[tmpWalksDictionary setObject:sortedDayArray forKey:dayKey];
}
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:#selector(localizedCompare:)];
self.days = [tmpDays sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
self.walks = [NSDictionary dictionaryWithDictionary:tmpWalksDictionary];
[tmpDays release];
[tmpWalksDictionary release];
[dateFormatter release];
self.success = YES;
} else {
self.success = NO;
self.errorString = [dictionary objectForKey:#"Error"];
}
if([dictionary objectForKey:#"Key"]) {
self.key = [dictionary objectForKey:#"Key"];
}
} else {
self.errorString = [error localizedDescription];
if(!self.errorString) {
self.errorString = #"Unknown Error";
}
self.success = NO;
}
[pool release];
}
-(NSString*)postData {
NSMutableString* postData = [[[NSMutableString alloc] init] autorelease];
[postData appendFormat:#"%#=%#", #"LoginKey", self.key];
return [NSString stringWithString:postData];
}
----
#implementation RequestHelper
+(NSDictionary*)performPostRequest:(NSString*)urlString:(NSString*)postData:(NSError**)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#", kHostName, urlString]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[urlRequest setHTTPMethod:#"POST"];
if(postData && ![postData isEqualToString:#""]) {
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
[urlRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
[urlRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
}
NSURLResponse *response = nil;
error = nil;
NSData *jsonData = [NSURLConnection sendSynchronousRequest:(NSURLRequest *)urlRequest returningResponse:(NSURLResponse **)&response error:(NSError **)&error];
NSString *jsonString = [[NSString alloc] initWithBytes: [jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(#"JSON: %#",jsonString);
//parse JSON
NSDictionary *dictionary = nil;
if([jsonData length] > 0) {
dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:error];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
return dictionary;
}
If I have the autorelease pool in place, the crash occurs on [pool release]. If I don't, then the crash just looks to appear in the main.m method, and I don't seem to get any useful information. It's difficult to track down when I have to wait 10 mins in between every test!
If anyone can offer any clues or directions to go, that'd be much appreciated.
It's almost certain you're overreleasing something in your code, seeing that the crash is occurring during a [pool release] (There's a autorelease pool in the main method as well).
You can find it using Xcode - use build and analyze to have the static analyser pinpoint potential problems. Run it and post the results.
try this:
http://cocoadev.com/index.pl?NSZombieEnabled
also, you should avoid:
1) calling UIKit methods from secondary threads
2) making (synchronous) url requests from the main thread.
you must be doing one in any case in RequestHelper's performPostRequest method.
My guess is this section
GetHistoryOperation* operation = (GetHistoryOperation*)object;
if(operation.success) {
[self performSelectorOnMainThread:#selector(loadHistorySuceeded:) withObject:operation waitUntilDone:YES];
} else {
[self performSelectorOnMainThread:#selector(loadHistoryFailed:) withObject:operation waitUntilDone:YES];
}
If the sleep happens at a bad point here, you have an object being passed to another thread. I'd find a way around having to pass the operation as the object.
This is a really old question, so sorry for the dredge, but there is no accepted answer.
I was also getting a EXC_BAD_ACCESS on NSOperationQueue -addOperation for seemingly no reason, and after a few days of hunting down memory leaks, and turning on all the debugger options i could find (malloc guard, zombies) and getting nothing, I found an NSLog warning that said: "[NSoperation subclass] set to IsFinished before being started by the queue."
When I modified my base operation subclass, so that its -cancel function only set (IsRunning = NO) and (IsFinished = YES) IF AND ONLY IF (IsRunning == YES), NSOperationQueue stopped crashing.
So if you're ever calling NSOperationQueue -cancelAllOperations, or you're doing that manually (i.e. for (NSOperation *op in queue.allOperations) ) double check to make sure that you don't set IsFinished on those operations in your subclass implementation.

How can I disable a button while NSXMLParser runs, then enable it when it completes?

I have a viewController which imports XMLParser.h as the class xmlParser
I'm passing an NSURL object in my viewController to the xmlParser class with the getXML method below
goButton is the button I tap to call the getXML method below. I disable the button which I tapped to trigger the getXML method, but I'm not sure where to put the code to enable it again once the xmlParser has finished parsing the returned XML.
- (IBAction) getXML {
goButton.enabled = NO;
// allocate and initialize the xmlParser
xmlParser = [[XMLParser alloc] init];
// then generate the URL we are going to pass to it and call the fetchXML method passing the URL.
NSURL *xmlurl = [[NSURL alloc] initWithString:#"http://www.mysite.com/myfile.xml"];
[xmlParser fetchXMLFromURL:xmlurl];
// release objects
[xmlurl release];
[xmlParser release];
}
As per #Squeegy recommendation, I modified my code.
- (IBAction) getXML {
goButton.enabled = NO;
xmlParser = [[XMLParser alloc] init];
[self performSelectorInBackground:#selector(parseInBackground:) withObject:xmlParser];
}
- (void)parseInBackground:(XMLParser*)parser {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *xmlurl = [[NSURL alloc] initWithString:#"http://www.mysite.com/myfile.xml"];
[parser fetchXMLFromURL:xmlurl];
[self performSelectorOnMainThread:#selector(didFinishXMLParsing:) withObject:parser];
[xmlurl release];
[pool drain];
}
- (void)didFinishXMLParsing:(NSXMLParser*)parser {
goButton.enabled = YES;
}
Looks to be working until it gets to the line
[self performSelectorOnMainThread:#selector(didFinishXMLParsing:) withObject:parser];
The compiler complains as follows:
2010-02-17 00:22:20.574 XMLApp[2443:521b] *** -[viewController performSelectorOnMainThread:withObject:]: unrecognized selector sent to instance 0x1285a0
2010-02-17 00:22:20.578 XMLApp[2443:521b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[viewController performSelectorOnMainThread:withObject:]: unrecognized selector sent to instance 0x1285a0'
2010-02-17 00:22:20.583 XMLApp[2443:521b] Stack: (
861696817,
860329709,
861700631,
861203093,
861166272,
18715,
846004025,
845672609,
848189713
)
- (IBAction)getXML {
goButton.enabled = NO;
xmlParser = [[XMLParser alloc] init];
NSURL *xmlurl = [[NSURL alloc] initWithString:#"http://www.mysite.com/myfile.xml"];
[xmlParser fetchXMLFromURL:xmlurl];
[self performSelectorInBackground:#selector(parseInBackground) withObject:xmlParser];
[xmlurl release];
[xmlParser release];
}
- (void)parseInBackground:(NSXMLParser*)parser {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[parser parse];
[self performSelectorOnMainThread:#selector(didFinishXMLParsing:)
withObject:parser
waitUntilDone:NO];
[pool drain];
}
- (void)didFinishXMLParsing:(NSXMLParser*)parser {
goButton.enabled = YES;
}
The trick is to do the processing on a background thread, which allows the UI to do stuff. When parsing is done, you have to make any UI changes back on the main thread.
When the parser finishes parsing, it will call it's delegate's:
- (void)parserDidEndDocument:(NSXMLParser *)parser
In that method, you can re-enable the button. You should probably do so with a performSelectorInMainThread call, since it involves changing a view.