2012-02-24 52 views
1

我開發使用的是iOS 5核心數據和大中央調度內部不一致

我使用大中央調度,以填補一個GMGridViewCell視圖有麻煩的iOS應用。

問題不在於GridCell本身,而在於訪問GCD中的數據。

這裏是我的代碼:

- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index 
{ 
    //NSLog(@"Creating view indx %d", index); 

    CGSize size = [self sizeForItemsInGMGridView:gridView]; 

    GMGridViewCell *cell = [gridView dequeueReusableCell]; 

    if (!cell) 
    { 
     cell = [[GMGridViewCell alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]; 
    } 

    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

    dispatch_queue_t fillCellQueue = dispatch_queue_create("Cell fetch queue", NULL); 
    dispatch_async(fillCellQueue, ^{ 
     SearchGridViewCell *cellView = [UIView loadFromNib:@"SearchGridViewCell" owner:self]; 
     Item *item = [self.foundItems objectAtIndex:index]; 

     cellView.itemImageView.image = [UIImage imageWithData:item.image.thumb]; 

     cellView.itemNameLabel.text = item.name; 

     cellView.brandImageView.image = [UIImage imageWithData:item.group3.image.thumb]; 

     Attribute *itemAttribute = [item.attributes objectAtIndex:0]; 
     cellView.attributeLabel.text = [itemAttribute.name stringByAppendingFormat:@": "]; 
     [cellView.attributeLabel sizeToFit]; 

     cellView.itemAttributeValueLabel.text = itemAttribute.value; 
     [cellView.itemAttributeValueLabel sizeToFit]; 

     dispatch_sync(dispatch_get_main_queue(), ^{ 
      [cell addSubview:cellView]; 
     }); 
    }); 
    dispatch_release(fillCellQueue); 

    return cell; 
} 

當運行應用程序,我得到了以下錯誤:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'statement is still active' 
*** First throw call stack: 
(0x1a1f052 0x35b3d0a 0x11cde0a 0x11cd58d 0x11f689f 0x11ec955 0x11d7df4 0x11f6418 0x11f3b62 0x11f3a57 0x11f316b 0x11f2f97 0x11f2b75 0x11f29f2 0x1236e10 0x51de7 0x44ab445 0x44acecf 0x44acd28 0x44ac4af 0x91442b24 0x914446fe) 

我在做什麼錯?

編輯更多信息,

拋出異常的第一行:

cellView.itemImageView.image = [UIImage imageWithData:item.image.thumb]; 

而且我相信這個問題是從GCD,因爲當我運行這個沒有GCD它工作正常。但網格的滾動有點遲緩,這就是爲什麼我要添加GCD的原因。

+0

您需要添加更多詳細信息。代碼中發生這種崩潰的位置?你怎麼知道上面的方法是罪魁禍首? – Rog 2012-02-24 20:15:51

+0

@Rog我已添加更多信息。基本上這不會在使用GCD時工作正常,但滾動有點遲緩。 – 2012-02-24 20:42:35

+0

你也不應該dispatch_sync()到主隊列,你應該提交所有請求到異步隊列(主隊列的行爲不是你認爲的)。 – jkh 2012-02-25 06:20:24

回答

6

我相信問題是self.foundItems,我猜是在另一個線程NSFetchRequest請求的結果。線程之間不能傳遞NSManagedObject。您必須在您要使用它的相同線程中獲取對象。

+0

我相信是的。我會研究這一點。 – 2012-02-24 21:36:05

+0

@chris,我需要爲每個單元獲取一個項目嗎?它使我的tableView呆滯。 – 2013-09-05 08:07:02

+0

@ValentinShamardin你最好的選擇就是在你將要使用的同一線程中獲取物品。 – chris 2013-09-06 10:26:09