NSOperationCount显示不正确的号码

时间:2014-03-05 13:03:29

标签: objective-c multithreading nsoperation nsoperationqueue

我无法理解为什么队列中的操作计数如此奇怪。我有一个队列,我在其中添加操作。添加后,我打印add:并请求哈希。然后我在发送中发生了processingBlock AFNetworking - AFImageRequestOperation - imageRequestOperationWithRequest:imageProcessingBlock:success:failure:

imageProcessingBlock开始时,我打印process, hash

success块上打印done, hash

failure阻止 - fail, hash

我还在一个操作queue.operationCount上调用取消之前将队列op1=的计数打印为if (![operation isExecuting]) [operation cancel]。可能会发生操作为nil(已经完成或不存在),因此查看op1=并不意味着事先取消了先前的操作。如果可以,我们就看不到processdone/fail日志,而我们可以看到它。

我的队列设置了queue.maxConcurrentOperationCount = 1;属性。

日志:

2014-03-05 13:14:06.800 app[7149:60b] add: 2889023970
2014-03-05 13:14:07.099 app[7149:60b] op1=0
2014-03-05 13:14:07.102 app[7149:60b] add: 2889023970
2014-03-05 13:14:07.267 app[7149:60b] op1=0
2014-03-05 13:14:07.271 app[7149:60b] add: 702670786
2014-03-05 13:14:07.431 app[7149:60b] op1=1
2014-03-05 13:14:07.435 app[7149:60b] add: 291730858
2014-03-05 13:14:07.601 app[7149:60b] op1=1
2014-03-05 13:14:07.603 app[7149:60b] add: 151251145
2014-03-05 13:14:07.703 app[7149:3903] process, 2889023970
2014-03-05 13:14:07.798 app[7149:60b] op1=1
2014-03-05 13:14:07.800 app[7149:60b] add: 3495583422
2014-03-05 13:14:07.982 app[7149:60b] op1=1
2014-03-05 13:14:07.984 app[7149:60b] add: 3951083409
2014-03-05 13:14:08.183 app[7149:60b] op1=1
2014-03-05 13:14:08.185 app[7149:60b] add: 3097363895
2014-03-05 13:14:08.366 app[7149:60b] op1=1
2014-03-05 13:14:08.369 app[7149:60b] add: 3940561568
2014-03-05 13:14:08.565 app[7149:60b] op1=0
2014-03-05 13:14:08.568 app[7149:60b] add: 3549899699
2014-03-05 13:14:08.703 app[7149:1603] process, 2889023970
2014-03-05 13:14:08.717 app[7149:60b] done, 2889023970
2014-03-05 13:14:08.720 app[7149:3903] process, 702670786
2014-03-05 13:14:08.764 app[7149:60b] op1=0
2014-03-05 13:14:08.767 app[7149:60b] add: 2257512580
2014-03-05 13:14:08.932 app[7149:60b] op1=0
2014-03-05 13:14:08.934 app[7149:60b] add: 1269923969
2014-03-05 13:14:09.099 app[7149:60b] op1=1
2014-03-05 13:14:09.101 app[7149:60b] add: 1717452702
2014-03-05 13:14:09.264 app[7149:60b] op1=0
2014-03-05 13:14:09.266 app[7149:60b] add: 3204563151
2014-03-05 13:14:09.448 app[7149:60b] op1=0
2014-03-05 13:14:09.450 app[7149:60b] add: 914226193
2014-03-05 13:14:09.648 app[7149:60b] op1=0
2014-03-05 13:14:09.649 app[7149:60b] add: 4288960663
2014-03-05 13:14:09.864 app[7149:60b] op1=0
2014-03-05 13:14:09.868 app[7149:60b] add: 1010721546
2014-03-05 13:14:09.908 app[7149:60b] done, 2889023970
2014-03-05 13:14:10.081 app[7149:60b] op1=0
2014-03-05 13:14:10.083 app[7149:60b] add: 1346065135
2014-03-05 13:14:10.298 app[7149:60b] op1=0
2014-03-05 13:14:10.301 app[7149:60b] add: 1610846086
2014-03-05 13:14:10.413 app[7149:60b] done, 702670786
2014-03-05 13:14:10.531 app[7149:60b] op1=0
2014-03-05 13:14:10.534 app[7149:60b] add: 1279065803
2014-03-05 13:14:10.748 app[7149:60b] op1=0
2014-03-05 13:14:10.749 app[7149:60b] add: 56514476
2014-03-05 13:14:10.949 app[7149:60b] op1=0
2014-03-05 13:14:10.951 app[7149:60b] add: 1647541451
2014-03-05 13:14:11.132 app[7149:60b] op1=0
2014-03-05 13:14:11.134 app[7149:60b] add: 1464536901
2014-03-05 13:14:11.297 app[7149:60b] op1=0
2014-03-05 13:14:11.300 app[7149:60b] add: 3032567003
2014-03-05 13:14:11.464 app[7149:60b] op1=0
2014-03-05 13:14:11.466 app[7149:60b] add: 1916037766
2014-03-05 13:14:11.535 app[7149:3903] process, 291730858
2014-03-05 13:14:11.648 app[7149:60b] op1=0
2014-03-05 13:14:11.650 app[7149:60b] add: 3051826257
2014-03-05 13:14:11.848 app[7149:60b] op1=0
2014-03-05 13:14:11.850 app[7149:60b] add: 3076241399
2014-03-05 13:14:11.907 app[7149:1603] process, 151251145
2014-03-05 13:14:11.952 app[7149:3c03] process, 3495583422
2014-03-05 13:14:12.098 app[7149:60b] op1=0
2014-03-05 13:14:12.100 app[7149:60b] add: 48397485
2014-03-05 13:14:12.331 app[7149:60b] op1=0
2014-03-05 13:14:12.334 app[7149:60b] add: 3887034591
2014-03-05 13:14:12.548 app[7149:60b] op1=0
2014-03-05 13:14:12.551 app[7149:60b] add: 898950083
2014-03-05 13:14:12.711 app[7149:7b27] process, 3951083409
2014-03-05 13:14:12.734 app[7149:60b] op1=1
2014-03-05 13:14:12.737 app[7149:60b] add: 3912316789
2014-03-05 13:14:12.931 app[7149:60b] op1=0
2014-03-05 13:14:12.935 app[7149:60b] add: 1407228822
2014-03-05 13:14:13.131 app[7149:60b] op1=0
2014-03-05 13:14:13.135 app[7149:60b] add: 3336394733
2014-03-05 13:14:13.331 app[7149:60b] op1=0
2014-03-05 13:14:13.335 app[7149:60b] add: 590013065
2014-03-05 13:14:13.550 app[7149:60b] op1=0
2014-03-05 13:14:13.553 app[7149:60b] add: 1365020375
2014-03-05 13:14:13.765 app[7149:60b] op1=0
2014-03-05 13:14:13.768 app[7149:60b] add: 3533463993
2014-03-05 13:14:13.788 app[7149:60b] done, 291730858
2014-03-05 13:14:14.014 app[7149:60b] op1=0
2014-03-05 13:14:14.018 app[7149:60b] add: 3276861409
2014-03-05 13:14:14.231 app[7149:60b] op1=0
2014-03-05 13:14:14.233 app[7149:60b] add: 110569377
2014-03-05 13:14:14.438 app[7149:60b] done, 151251145
2014-03-05 13:14:14.448 app[7149:60b] op1=0
2014-03-05 13:14:14.451 app[7149:60b] add: 3082863482
2014-03-05 13:14:14.507 app[7149:60b] done, 3495583422
2014-03-05 13:14:14.664 app[7149:60b] op1=0
2014-03-05 13:14:14.667 app[7149:60b] add: 2880825480
2014-03-05 13:14:14.881 app[7149:60b] op1=0
2014-03-05 13:14:14.885 app[7149:60b] add: 174982724
2014-03-05 13:14:15.081 app[7149:60b] op1=0
2014-03-05 13:14:15.084 app[7149:60b] add: 3595970015
2014-03-05 13:14:15.130 app[7149:60b] done, 3951083409
2014-03-05 13:14:15.432 app[7149:60b] op1=0
2014-03-05 13:14:15.450 app[7149:60b] add: 2167277765
2014-03-05 13:14:15.698 app[7149:1603] process, 3940561568
2014-03-05 13:14:15.731 app[7149:60b] op1=0
2014-03-05 13:14:15.735 app[7149:60b] add: 1556977129
2014-03-05 13:14:16.018 app[7149:60b] op1=0
2014-03-05 13:14:16.021 app[7149:60b] add: 2295161098
2014-03-05 13:14:16.265 app[7149:60b] op1=0
2014-03-05 13:14:16.268 app[7149:60b] add: 3293570222
2014-03-05 13:14:16.465 app[7149:60b] op1=0
2014-03-05 13:14:16.468 app[7149:60b] add: 1495040366
2014-03-05 13:14:16.648 app[7149:60b] op1=0
2014-03-05 13:14:16.650 app[7149:60b] add: 2960130674
2014-03-05 13:14:16.848 app[7149:60b] op1=0
2014-03-05 13:14:16.850 app[7149:60b] add: 2192695544
2014-03-05 13:14:17.031 app[7149:60b] op1=0
2014-03-05 13:14:17.034 app[7149:60b] add: 875349923
2014-03-05 13:14:17.248 app[7149:60b] op1=0
2014-03-05 13:14:17.250 app[7149:60b] add: 550530235
2014-03-05 13:14:17.498 app[7149:60b] op1=0
2014-03-05 13:14:17.501 app[7149:60b] add: 3315589548
2014-03-05 13:14:17.588 app[7149:60b] done, 3940561568
2014-03-05 13:14:17.591 app[7149:1603] process, 3097363895
2014-03-05 13:14:17.731 app[7149:60b] op1=0
2014-03-05 13:14:17.733 app[7149:60b] add: 3276473474
2014-03-05 13:14:17.981 app[7149:60b] op1=0
2014-03-05 13:14:17.984 app[7149:60b] add: 1965226584
2014-03-05 13:14:18.215 app[7149:60b] op1=0
2014-03-05 13:14:18.217 app[7149:60b] add: 2261494312
2014-03-05 13:14:18.431 app[7149:60b] op1=0
2014-03-05 13:14:18.433 app[7149:60b] add: 683653812
2014-03-05 13:14:18.631 app[7149:60b] op1=0
2014-03-05 13:14:18.634 app[7149:60b] add: 959046345
2014-03-05 13:14:18.864 app[7149:60b] op1=0
2014-03-05 13:14:18.867 app[7149:60b] add: 3287457211
2014-03-05 13:14:19.081 app[7149:60b] op1=0
2014-03-05 13:14:19.084 app[7149:60b] add: 406162445
2014-03-05 13:14:19.245 app[7149:4e0b] process, 3549899699
2014-03-05 13:14:19.331 app[7149:60b] op1=0
2014-03-05 13:14:19.333 app[7149:60b] add: 3959798522
2014-03-05 13:14:19.342 app[7149:3c03] process, 2257512580
2014-03-05 13:14:19.459 app[7149:7b27] process, 1717452702
2014-03-05 13:14:19.547 app[7149:60b] op1=0
2014-03-05 13:14:19.549 app[7149:60b] add: 4189223064
2014-03-05 13:14:19.765 app[7149:60b] op1=0
2014-03-05 13:14:19.769 app[7149:60b] add: 3605790954
2014-03-05 13:14:19.964 app[7149:60b] op1=0
2014-03-05 13:14:19.966 app[7149:60b] add: 2927423940
2014-03-05 13:14:20.008 app[7149:60b] done, 3097363895
2014-03-05 13:14:20.011 app[7149:1603] process, 1269923969
2014-03-05 13:14:20.228 app[7149:3903] process, 3204563151
2014-03-05 13:14:20.312 app[7149:60b] op1=0
2014-03-05 13:14:21.050 app[7149:60b] Received memory warning.
2014-03-05 13:14:21.207 app[7149:862b] process, 914226193
2014-03-05 13:14:21.650 app[7149:7d0b] process, 4288960663
2014-03-05 13:14:21.707 app[7149:60b] add: 3605790954
2014-03-05 13:14:21.717 app[7149:60b] op1=1
2014-03-05 13:14:21.723 app[7149:60b] add: 3605790954
2014-03-05 13:14:21.731 app[7149:60b] op1=2
2014-03-05 13:14:21.740 app[7149:60b] add: 3605790954
2014-03-05 13:14:21.749 app[7149:60b] op1=3
2014-03-05 13:14:21.759 app[7149:60b] add: 3605790954
2014-03-05 13:14:21.764 app[7149:60b] op1=4
2014-03-05 13:14:21.780 app[7149:60b] add: 3605790954
2014-03-05 13:14:22.198 app[7149:60b] op1=0
2014-03-05 13:14:22.200 app[7149:60b] add: 4189223064
2014-03-05 13:14:22.415 app[7149:60b] op1=0
2014-03-05 13:14:22.417 app[7149:60b] add: 3959798522
2014-03-05 13:14:22.420 app[7149:60b] done, 1717452702
2014-03-05 13:14:22.422 app[7149:7b27] process, 1010721546
2014-03-05 13:14:22.648 app[7149:60b] op1=0
2014-03-05 13:14:22.650 app[7149:60b] add: 406162445
2014-03-05 13:14:22.768 app[7149:60b] done, 3549899699
2014-03-05 13:14:22.784 app[7149:60b] done, 2257512580
2014-03-05 13:14:22.787 app[7149:3c03] process, 1346065135
2014-03-05 13:14:22.898 app[7149:60b] op1=0
2014-03-05 13:14:22.900 app[7149:60b] add: 3287457211
2014-03-05 13:14:23.115 app[7149:60b] op1=0
2014-03-05 13:14:23.120 app[7149:60b] add: 959046345
2014-03-05 13:14:23.331 app[7149:60b] op1=0
2014-03-05 13:14:23.333 app[7149:60b] add: 683653812
2014-03-05 13:14:23.499 app[7149:60b] done, 1269923969
2014-03-05 13:14:23.531 app[7149:60b] op1=0
2014-03-05 13:14:23.535 app[7149:60b] add: 2261494312
2014-03-05 13:14:23.749 app[7149:60b] op1=0
2014-03-05 13:14:23.752 app[7149:60b] add: 1965226584
2014-03-05 13:14:23.799 app[7149:60b] done, 3204563151
2014-03-05 13:14:23.966 app[7149:60b] op1=0
2014-03-05 13:14:23.968 app[7149:60b] add: 3276473474
2014-03-05 13:14:24.198 app[7149:60b] op1=0
2014-03-05 13:14:24.202 app[7149:60b] add: 3315589548
2014-03-05 13:14:24.267 app[7149:60b] done, 914226193
2014-03-05 13:14:24.270 app[7149:862b] process, 1610846086
2014-03-05 13:14:24.415 app[7149:60b] op1=0
2014-03-05 13:14:24.417 app[7149:60b] add: 550530235
2014-03-05 13:14:24.631 app[7149:60b] op1=0
2014-03-05 13:14:24.635 app[7149:60b] add: 875349923
2014-03-05 13:14:24.848 app[7149:60b] op1=0
2014-03-05 13:14:24.851 app[7149:60b] add: 2192695544
2014-03-05 13:14:25.031 app[7149:60b] done, 4288960663
2014-03-05 13:14:25.031 app[7149:7d0b] process, 1279065803
2014-03-05 13:14:25.099 app[7149:60b] op1=0
2014-03-05 13:14:25.101 app[7149:60b] add: 2960130674
2014-03-05 13:14:25.320 app[7149:4e0b] process, 56514476
2014-03-05 13:14:25.367 app[7149:60b] op1=0
2014-03-05 13:14:25.370 app[7149:60b] add: 1495040366
2014-03-05 13:14:25.600 app[7149:60b] op1=0
2014-03-05 13:14:25.602 app[7149:60b] add: 3293570222
2014-03-05 13:14:25.783 app[7149:60b] done, 1010721546
2014-03-05 13:14:25.815 app[7149:60b] op1=0
2014-03-05 13:14:25.818 app[7149:60b] add: 2295161098
2014-03-05 13:14:26.065 app[7149:60b] op1=0
2014-03-05 13:14:26.069 app[7149:60b] add: 1556977129
2014-03-05 13:14:26.243 app[7149:60b] done, 1346065135
2014-03-05 13:14:26.298 app[7149:60b] op1=0
2014-03-05 13:14:26.302 app[7149:60b] add: 2167277765
2014-03-05 13:14:26.548 app[7149:60b] op1=0
2014-03-05 13:14:26.552 app[7149:60b] add: 3595970015
2014-03-05 13:14:26.782 app[7149:60b] op1=0
2014-03-05 13:14:26.785 app[7149:60b] add: 174982724
2014-03-05 13:14:27.148 app[7149:60b] op1=0
2014-03-05 13:14:27.188 app[7149:60b] add: 2880825480
2014-03-05 13:14:27.448 app[7149:60b] op1=0
2014-03-05 13:14:27.451 app[7149:60b] add: 3082863482
2014-03-05 13:14:27.558 app[7149:60b] done, 1610846086
2014-03-05 13:14:27.631 app[7149:60b] op1=0
2014-03-05 13:14:27.633 app[7149:60b] add: 110569377
2014-03-05 13:14:27.805 app[7149:60b] op1=1
2014-03-05 13:14:27.811 app[7149:60b] add: 3276861409
2014-03-05 13:14:27.969 app[7149:60b] op1=1
2014-03-05 13:14:27.974 app[7149:60b] add: 3533463993
2014-03-05 13:14:28.101 app[7149:60b] op1=1
2014-03-05 13:14:28.111 app[7149:60b] add: 1365020375
2014-03-05 13:14:28.249 app[7149:60b] op1=1
2014-03-05 13:14:28.254 app[7149:60b] add: 590013065
2014-03-05 13:14:28.400 app[7149:60b] op1=1
2014-03-05 13:14:28.403 app[7149:60b] add: 3336394733
2014-03-05 13:14:28.518 app[7149:60b] Received memory warning.
2014-03-05 13:14:28.532 app[7149:3c03] process, 1647541451
2014-03-05 13:14:28.632 app[7149:60b] op1=0
2014-03-05 13:14:28.637 app[7149:60b] add: 1407228822
2014-03-05 13:14:28.704 app[7149:60b] done, 1279065803
2014-03-05 13:14:28.716 app[7149:60b] op1=1
2014-03-05 13:14:28.720 app[7149:60b] add: 3912316789
2014-03-05 13:14:28.884 app[7149:60b] op1=1
2014-03-05 13:14:28.889 app[7149:60b] add: 898950083
2014-03-05 13:14:29.023 app[7149:60b] done, 56514476
2014-03-05 13:14:29.068 app[7149:60b] op1=1
2014-03-05 13:14:29.074 app[7149:60b] add: 3887034591
2014-03-05 13:14:29.233 app[7149:60b] op1=1
2014-03-05 13:14:29.237 app[7149:60b] add: 48397485
2014-03-05 13:14:29.384 app[7149:60b] op1=1
2014-03-05 13:14:29.391 app[7149:60b] add: 3076241399
2014-03-05 13:14:29.555 app[7149:60b] op1=1
2014-03-05 13:14:29.561 app[7149:60b] add: 3051826257
2014-03-05 13:14:29.734 app[7149:60b] op1=1
2014-03-05 13:14:29.737 app[7149:60b] add: 1916037766
2014-03-05 13:14:29.902 app[7149:60b] op1=1
2014-03-05 13:14:29.904 app[7149:60b] add: 3032567003
2014-03-05 13:14:30.081 app[7149:60b] op1=0
2014-03-05 13:14:30.083 app[7149:60b] add: 1464536901
2014-03-05 13:14:30.248 app[7149:60b] op1=0
2014-03-05 13:14:30.250 app[7149:60b] add: 1647541451
2014-03-05 13:14:30.417 app[7149:60b] op1=1
2014-03-05 13:14:30.432 app[7149:60b] add: 1647541451
2014-03-05 13:14:30.661 app[7149:60b] op1=0

现在,您可以注意到,很多对象都添加到队列中。我无法理解为什么操作计数如此之低 - 添加5次操作后它应该是5 ..?我知道它可能发生在不同的线程中,但正如您所看到的,它永远不足以涵盖所有这些添加的操作。

据我所知,count应该显示添加到队列中的所有操作,而不仅仅是工作。

我认为我的理解正在发生的事情几乎为零,欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

我对AFNetworking并不熟悉,但我在NSURLConnection中看到了同样的事情。 我的想法是观察queue.operationCount以显示网络活动指示符,如果它不为空。但我正在做异步请求,它是怎么回事:首先执行发送请求操作然后没有队列中的操作,因为我们等待响应异常,然后设备重新发送响应并在队列中出现下载响应操作,然后在队列中出现执行响应的操作块处理程序。