0

我有一个本地 SQLite 数据库,需要返回大量记录。加载需要几秒钟,所以我想添加一个活动指示器。活动指示器似乎在正常运行,但问题是池不允许数组返回任何值,见下文。

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

当我查看调试器时,我看到随着活动指示器的进行而构建数组。问题是我认为池释放也在释放我的阵列。

当我不添加池和活动指示器时,代码可以很好地在表视图中返回查询。

如果这就是这里发生的事情,任何人都可以帮助我指出正确的方向,不要释放阵列吗?任何形式的帮助将不胜感激。:-)

---另外--- 环顾四周后,我发现我必须将数组传递回主线程。这是我从网上搜索收集到的。

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

我将如何使用这些数组加载表?

4

3 回答 3

0

不确定您的池是否会释放数组,您的代码似乎很直接。您可能想检查您是否真的从数据库中获取任何数据。

但是,我注意到您停止在后台线程中为活动指示器设置动画。UI 活动应该在主线程中完成。我昨晚刚刚完成了这个编码,并使用这个线程解决了我的问题。将停止动画代码放入 performSelectorOnMainThread 指定的选择器中。

于 2012-07-06T05:43:10.170 回答
0

字符串很好,因为它们被添加到数组中。

看起来您的数组正在被释放,但您没有显示它的代码。

你是如何创建数组本身的?如果它是自动释放的,它将在您耗尽池时被释放。您应该“保留”它而不是“自动释放”它。

于 2012-07-06T14:00:31.840 回答
0

尝试[tableview reloadData]在末尾添加-(void)loadData method

于 2012-08-28T18:37:53.843 回答