iphone - 不明白为什么我的 GCD tableview 崩溃了

标签 iphone ios objective-c uitableview grand-central-dispatch

我刚刚重写了这篇文章,涵盖了我能想到的每一个可能的角度。我不知道为什么会崩溃。也许有人可以帮我弄清楚。

这是 cellForRowAtIndexPath 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    JHomeViewCell *cell = (JHomeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[JHomeViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:CellIdentifier];
        cell.delegate = self;
    }

    cell.cellContent.cellInfo = [self cellInfoForCellAtIndexPath:indexPath];
    if (cell.cellContent.cellInfo.thumbnailsComplete == YES || cell.cellContent.cellInfo.thumbnailsBeingCreated == YES) {
        [cell.cellContent setNeedsDisplay];
    }
    else {
        [cell.cellContent setup];
    }


    return cell;
}

在 cellContent 中,有这样的设置方法:

-(void)setup {

    [self setNeedsDisplay];

    self.cellInfo.thumbnailsBeingCreated = YES;

    NSManagedObjectID *entryID = self.cellInfo.objectID;

    dispatch_queue_t cellSetupQueue = dispatch_queue_create("com.Journalized.SetupCell", NULL);
    dispatch_async(cellSetupQueue, ^{
        NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];

        NSPersistentStoreCoordinator *coordinator = [[CoreDataStore mainStore] context].persistentStoreCoordinator;

        [newMoc setPersistentStoreCoordinator:coordinator];
        NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
        [notify addObserver:self 
                   selector:@selector(mergeChanges:) 
                       name:NSManagedObjectContextDidSaveNotification 
                     object:newMoc];

        Entry *entry = (Entry *)[newMoc objectWithID:entryID];

        [newMoc save:nil];
        int i = 0;

        while (i < self.cellInfo.numberOfThumbnailsToDraw) {
        NSLog(@"number of thumbnails: %i %i %i", self.cellInfo.numberOfThumbnailsToDraw, entry.media.count, i);
            Media *media = [entry.media objectAtIndex:i];
            UIImage *image = [media getThumbnail];

            BOOL success = [newMoc save:nil];

            //NSLog(@"time: %@ success: %i", entry.entryTableInfo.creationTimeString, success);

            [self.cellInfo.thumbnails setObject:image forKey:[NSNumber numberWithInt:i]];
            i++;
        }
        [[NSNotificationCenter defaultCenter] removeObserver:self];

        dispatch_async(dispatch_get_main_queue(), ^{

            self.cellInfo.thumbnailsComplete = YES;

            [self setNeedsDisplay];
        });

    });
    dispatch_release(cellSetupQueue);

它在线上崩溃了:

Media *media = [entry.media objectAtIndex:i];

出现错误:

index 1 beyond bounds [0 .. 0]

上面的 NSLog...

NSLog(@"number of thumbnails: %i %i %i", self.cellInfo.numberOfThumbnailsToDraw, entry.media.count, i);

给出结果:

number of thumbnails: 2 1 1

除了 [cellInfoForCellAtIndexPath:] 中设置的值外,哪种方式可以解释崩溃;方法,像这样:

cellInfo.numberOfMediaItems = entry.media.count;
cellInfo.numberOfThumbnailsToDraw = MIN(cellInfo.numberOfMediaItems, 3);

我真的不知道问题出在哪里,或者为什么会发生,但在这部分得到修复之前我无法继续使用我的应用程序。

最佳答案

numberOfThumbnailsToDraw 是 2,这意味着 while 循环将执行 0、1,但您的 entry.media 的计数仅为 1,因此它只有 0 索引,因此它当然会崩溃。

关于iphone - 不明白为什么我的 GCD tableview 崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10883913/

相关文章:

ios - 以编程方式将搜索栏和 UISearchDisplayController 添加到 UITableView

iphone - iPhone 是否有 3G 连接 "connectable?"使用套接字时 3g/wifi 是否透明?

ios - 即使在终止并重新启动应用程序后,如何在 native iOS 中下载音频文件?

ios - 健康套件 : how to determine original unit HKQuantitySample was stored in?

ios - 如何只更新 UITableViewCell 的一个元素?

ios - 如何测试 Firebase DataSnapshot 以模拟对象解析?

ios - 如何以编程方式为不同数量的 UIView 设置约束

iphone - 替换 不添加 : UICollectionView

iphone - Objective-C 中的全局常量

iphone - 应用程序刚刚批准在 iPad 上崩溃