簡體   English   中英

performBlock將充當操作隊列嗎?

[英]Will performBlock act as operation Queue?

我有一個NSMutableArray,它每20秒插入一次數據,它一次插入10條記錄。 我需要一次在子線程中保存這10條記錄。

  NSMutableArray *fill = <fill 10 records data in every 20 sec. >

   if [fill count] == 10) {
   [moc performBlock:^{
     for(data in fill)// Now there is 10 elements
    {
        //Fetch something
        //Process data
       //Save
     }
    }];
  fill =nil;
  }

但是,如果在塊的進程超過20秒,“ ”將開始插入,而現在“ ”有10+的元素,所以我的問題是它會影響塊的內部? 還是在塊填充內部還有10個元素?

首先,如果該塊的時間超過10s,並且在其他位置的填充數組中插入對象,則您仍在同時枚舉它。 這會導致異常,因為枚舉時不允許您進行修改。 復制數組並對其執行操作怎么樣?

就像是:

NSMutableArray *fill = <fill 10 records data in every 20 sec. >

if [fill count] == 10) {
  NSArray *fillCopy = [fill copy];
  [fill removeAllObjects];
  [moc performBlock:^{
     for(data in fillCopy)// Now there is 10 elements
     {
       //Fetch something
       //Process data
       //Save
     }
  }];
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM