performBlock会作为操作队列吗?

时间:2015-05-28 12:21:38

标签: ios core-data

我有一个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个以上元素,所以我的问题是它会影响块的内部吗?或者在 fill 块内还有10个元素?

1 个答案:

答案 0 :(得分:0)

首先,如果块的时间超过10秒并且您在其他地方的填充数组中插入对象,那么您仍然会同时枚举它。这会导致异常,因为在枚举时不允许修改。如何制作数组的副本并对其执行操作呢?

类似的东西:

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