Table view data source array deallocated after handling asynchronous loading of data

I'm pretty new to Objective-C and am having a problem updating my UITableView after an asynchronous call. I have a table view populated from an NSMutableArray; I populate the array from a JSON response from a .net web service. I'm using wsdl2objc to get the JSON from the web service.

Here is my code:

@interface NewsViewController : UITableViewController <ServiceSoapBindingResponseDelegate>

@property (nonatomic, strong) News *news;
@property (nonatomic, retain) NSMutableArray *newsList;

-(void) updateNewView:(NSMutableArray*) result;
-(void) loadNewsFromRemoteServer;

@end


-(void) viewDidLoad
{
    _newsList = [[NSMutableArray alloc] init];
    [self loadNewsFromRemoteServer];
    [super viewDidLoad];
}

-(void) loadNewsFromRemoteServer
{  
    NSString *customerID = @"xxx";
    NSString *uniqueID = @"xxx";    
    ServiceSoapBinding *bNews = [[ServiceSvc ServiceSoapBinding] retain];
    bNews.logXMLInOut = YES;
    ServiceSvc_GetNews *cRequest = [[ServiceSvc_GetNews new] autorelease];
    cRequest.id_ = customerID;
    cRequest.uniqueId = uniqueID;
    [bNews GetNewsAsyncUsingParameters:cRequest delegate:self];
}


-(void) operation:(ServiceSoapBindingOperation *)operation completedWithResponse:(ServiceSoapBindingResponse *)response
{
    NSArray *responseHeaders = response.headers;
    NSArray *responseBodyParts = response.bodyParts;
    NSMutableArray *newsListFromWebserver = [[NSMutableArray alloc] init];
    for(id header in responseHeaders) {
        // here do what you want with the headers, if there's anything of value in them
    }
    for(id bodyPart in responseBodyParts) {         
        if ([bodyPart isKindOfClass:[SOAPFault class]]) {
            // You can get the error like this:
            //NSLog(@"new list :: RESPONSE FROM SERVER :: %@",((SOAPFault *)bodyPart).simpleFaultString);
            continue;
        }
        //Get News List
        if([bodyPart isKindOfClass:[ServiceSvc_GetNewsResponse class]]) {
            ServiceSvc_GetNewsResponse *body = (ServiceSvc_GetNewsResponse*)bodyPart;
            NSString *nList = body.GetNewsResult;  //JSON FORMAT
            NSLog(@"new list :: RESPONSE FROM SERVER :: nList %@", nList);
            NSDictionary *resultsDictionary = [nList objectFromJSONString] ;
            for (NSDictionary * dataDict in resultsDictionary) {
                News *newNews = [[News alloc] init];
                _news = newNews;
                _news.newsTitle = [NSString stringWithFormat:[dataDict objectForKey:@"Title"]];
                _news.newsBody = [NSString stringWithFormat:[dataDict objectForKey:@"Body"]];
                _news.newsDate = [NSString stringWithFormat:[dataDict objectForKey:@"NewDate"]];
                [newsListFromWebserver addObject:_news];
               [_news release];
            }    
        }
    }   
    [self performSelectorOnMainThread:@selector(updateNewView:) withObject:newsListFromWebserver waitUntilDone:NO];
    [newsListFromWebserver release];
}

-(void) updateNewView:(NSMutableArray*) result
{
    _newsList = result;
    NSLog( @"new list - number of news :: %u", [_newsList count]);
    [self.tableView reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }    
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //Get the object from the array.
    News *cNews = [_newsList objectAtIndex:indexPath.row];
    cell.textLabel.text = cNews.newsDate;
    cell.detailTextLabel.text = cNews.newsTitle;
    cell.imageView.image = [UIImage imageNamed:@"bullet.png"];        
    return cell;
}

My application logs this to the console:

2012-09-08 22:36:45.463 xxx[45630:13a03] new list - number of news :: 1
2012-09-08 22:36:45.465 xxx[45630:13a03] *** -[__NSArrayM objectAtIndex:]: message sent to deallocated instance 0x79a3680

It fails calling [_newsList objectAtIndex:indexPath.row] in tableView:cellForRowAtIndexPath:. Any help? Thanks in advance

Answers


By the time updateNewView: method is called in the Main Thread newsListFromWebserver object created in the operation:completedWithResponse: method is released in the last line of that method and hence deallocated. The simplest way to fix the problem is to assign value to _newsList ivar inside operation:completedWithResponse: and after that call updateNewView in the main thread. Notice, that in that case you can remove the result argument from it.

Also there is a leak in the first line of updateNewView method.

Here is a small hint how it should look like:

-(void) operation:(ServiceSoapBindingOperation *)operation completedWithResponse:           (ServiceSoapBindingResponse *)response
{
    // your old parsing code here

    self.newsList = newsListFromWebserver;
    [newsListFromWebserver release];
    [self performSelectorOnMainThread:@selector(updateNewView) withObject:nil waitUntilDone:NO];
}

-(void) updateNewView
{
    NSLog( @"new list - number of news :: %u", [_newsList count]);
    [self.tableView reloadData];
}

Need Your Help

Powerbuilder Date Math

date report powerbuilder date-math

How do I subtract 30 days from a date in PowerBuilder?

do I need to destroy a string in c++

c++ stl

if I have a string in a class, then memory is allocated. Do I have to destroy the string in the destructor? e.g.

Python: Read binary file into buffer as integer then Slice

python binary integer slice

In python, how do i load a binary file into a buffer then read individual bytes as numbers not strings.?