2012-02-24 7 views
1

私はGMGridViewCellのビューを埋めるためにグランドセントラルディスパッチを使用してトラブルを抱えていますiOSの5コアデータとグランドセントラル派遣内部矛盾

を使って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) 

は私が間違って何をしているのですか?例外がスローされます

EDIT詳細情報、

最初の行:

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

そして私は、私はGCDせずにこれを実行すると、それが正常に動作しますので、問題はGCDからであると考えています。しかし、グリッドのスクロールはちょっと遅いので、GCDを追加したいのです。

+0

詳細を追加する必要があります。コード内でこのクラッシュが発生していますか?上記の方法が犯人であることをどうお知りになりますか? – Rog

+0

@Rog詳細を追加しました。基本的には、GCDを使用しない場合はうまく動作しますが、スクロールは少し遅いです。 –

+0

また、メインキューにdispatch_sync()を実行しないでください。すべての要求を非同期キューに提出する必要があります(メインキューの動作は、それとは異なります)。 – jkh

答えて

6

私はこの問題がself.foundItemsであると信じています。これは、別のスレッドでNSFetchRequestリクエストの結果であると推測しています。 NSManagedObjectをスレッド間で渡すことはできません。あなたはそれを使用しようとしている同じスレッド内のオブジェクトをフェッチする必要があります。

+0

私はそう信じています。私はこれを調べます。 –

+0

@chris、各セルに1つのアイテムを取得する必要はありますか?それは私のtableViewを低迷させる。 –

+0

@ValentinShamardinあなたがそれを使用している同じスレッドで項目をフェッチするだけです。 – chris