0

i have some problem with NSMutablearray and JSON parse. So what i doing? A make parse from JSON and send to my TableViewCell. I have my code:

h:

{
    NSMutableData *webdata;

    NSURLConnection *connection;

    NSMutableArray *array;

    NSMutableArray *array2;

NSTimer *timer;
}

m:

{
[super ViewDidload]

    [[self tableTrack] setDelegate:self];
    [[self tableTrack] setDataSource:self];
    array = [[NSMutableArray alloc] init];
    array2 = [[NSMutableArray alloc] init];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(plistGet) userInfo:nil repeats:YES];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webdata setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webdata appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error load");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
    NSDictionary *playlist =[allDataDictionary objectForKey:@"playlist"];

    for (NSDictionary *diction in playlist) {
        NSDictionary *artist = [diction objectForKey:@"artist"];
        NSDictionary *song = [diction objectForKey:@"song"];
        NSString *name = [artist objectForKey:@"name"];
        NSString *namesong = [song objectForKey:@"name"];
        [array addObject:name];
        [array2 addObject:namesong];
    }
    [[self tableTrack]reloadData];
}

-(void)plistGet {


    [array removeAllObjects];
    [array2 removeAllObjects];
    NSURL *url = [NSURL URLWithString:@"http://plist.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        webdata = [[NSMutableData alloc]init];
    }

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
//    return [array2 count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(! cell)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
    }

    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

    cell.textLabel.textColor = [UIColor colorWithRed:(50/255.0) green:(200/255.0) blue:(255/255.0) alpha:1];


    [tableTrack setBackgroundView:nil];

    tableTrack.backgroundColor = [UIColor clearColor];

   cell.detailTextLabel.font=[UIFont fontWithName: @"Helvetica" size:11];

    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 9.0 ];
    cell.textLabel.font  = myFont;

    self.tableTrack.separatorStyle = UITableViewCellSeparatorStyleNone;

   UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)];
    separatorView.layer.borderColor = [UIColor darkGrayColor].CGColor;
    separatorView.layer.borderWidth = 0.5;
    [cell.contentView addSubview:separatorView];

    return cell;

}

All work very good but after 5 or 3 minutes i have error and my app crashed.

Error:

2013-06-21 12:32:41.502 EuropaPlusUA[651:707] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' * First throw call stack: (0x353f188f 0x37798259 0x3533a9db 0xfa9d5 0x32e85efb 0x32e84fd9 0x32e84763 0x32e28f37 0x353501fb 0x32220aa5 0x322206bd 0x32224843 0x3222457f 0x3221c4b9 0x353c5b1b 0x353c3d57 0x353c40b1 0x353474a5 0x3534736d 0x36fe3439 0x32e53cd5 0xf8843 0xf87d0) terminate called throwing an exception

What is it? Help please.

3 Answers 3

1

Your problem is that you probably do another plistget method after that 3-5 minutes. That method will throw away all objects from your array. During the time your data is getting loaded and the time you cleared your array, the table datasource is empty, however you are trying to get this object from index 0. This is why your crash happens.

The solution however is simple. Do not call the removeAllObjects method at all. Simply replace the array with the contents that you will retrieve.

Replace mechanic that you will need to do the moment you get the data from your server:

NSMutableArray *tempDataArray = [[NSMutableArray alloc] init];
//put retrieved data from your web call in the tempDataArray
array = tempDataArray;
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry i am not understand something, This code where i must put?
I used [array removeAllObjects]; to because I do not need that information is added continuously to I need to make it a substitute for instance only 3 cell
0

me thinks the only objectAtIndex is here.

cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];

seems likely array was emptied by plistget

[array removeAllObjects];

Comments

0

Totumus Maximus is right.

If you just need to bridge the gap between getting clearing the arrays and reloading the data why not try NOT deleting them until the new data is ready? Then just replace them

Alternatively make a copy of them first (say to backUpArray and backUpArray2) and then check to see if you have data in array/array2 (something like if([array count]) or if (array) {...) before you try and load the cell.

If you have nothing in the arrays use [backUpArray lastObject] to give you some data for the (brief ?) time until the arrays are reloaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.