Вопрос

I have a local SQLite Database and need to return a large number of records. It takes a several seconds to load so I want to add an activity indicator. The activity indicator seems to be running as it should but the problem is the pool isn't allowing the arrays to return any value see below.

  - (void)viewDidLoad
    {
        [super viewDidLoad];

        activityIndicator = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
        activityIndicator.frame = CGRectMake(0.0, 0.0, 48.0, 48.0);
        activityIndicator.center = self.view.center;
        [self.view addSubview:activityIndicator];

        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
         [activityIndicator startAnimating];            


     [self performSelectorInBackground:@selector(loadData) withObject:nil];   

    }
//What I need to load from SQLITE
-(void)loadData {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Path to the database

//CODED HERE FOR DATABASE TO OPEN AND QUERY HERE TO BUILD ARRAYS

        NSString *firstField = [NSString stringWithUTF8String:cFirst];
                NSString *secondField = [NSString stringWithUTF8String:cSecond];
                NSString *thirdField = [NSString stringWithUTF8String:cThird];


                [FirstArray addObject:firstField];
        [SecondArray addObject:secondField];
                [ThirdArray addObject:thirdField];  

   //Checking to see if records are being added to the arrays
NSString *recordAdded = [NSString stringWithFormat:@"%@ - %@ - %@", firstField, secondField, thirdField];

                    NSLog(@"Song: %@", recordAdded);

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;     
    [activityIndicator stopAnimating];

  [pool release];   
}

- (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];

        NSString *firstValue = [firstArray objectAtIndex:indexPath.row]; 
        NSString *secondValue = [secondArray objectAtIndex:indexPath.row];
        NSString *thirdValue = [thirdArray objectAtIndex:indexPath.row];

        NSString *details = [NSString stringWithFormat:@"%@ - %@", secondValue, thirdValue];


        cell.textLabel.text = cellValue;

        cell.detailTextLabel.text = details ;

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

When I'm looking at the debugger I see that the arrays are being built as the activity indicator is going. The problem is that I think the pool release is releasing my arrays too.

The code returns the query in the table view fine when I don't add the Pool and activity indicator.

Can anyone help to point me in the right direction to not release the arrays if that's what is happening here? Any kind of help would be much appreciated. :-)

---Also--- After looking around more I found that I have to pass the arrays back to the main thread. This is what I gather from searching online.

[self performSelectorOnMainThread:@selector(done:) withObject:cellData waitUntilDone:NO];

How would I go about loading the table with these arrays?

Это было полезно?

Решение

Not sure about your pool releasing the array, your code seems pretty direct. You may want to check whether you are really getting any data from your database.

However, I notice that you stop animating the activity indicator in the background thread. UI activity should be done in the main thread. I just did this coding last night and solved my problem using this thread. Put your stop animating code in the selector specified by performSelectorOnMainThread.

Другие советы

The strings are fine because they are being added to the array.

It looks like your array is being released, but you don't show the code for it.

How are you creating the array itself? If it is autoreleased, it will be deallocated when you drain the pool. You should "retain" it instead of "auotrelease"ing it.

Try to add [tableview reloadData] at the end of the -(void)loadData method

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top