Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSOperationQueue超出范围异常_Iphone - Fatal编程技术网

Iphone NSOperationQueue超出范围异常

Iphone NSOperationQueue超出范围异常,iphone,Iphone,我最近犯了一个奇怪的错误。NSOperationQueue表示其中有1个对象,但我无法访问其中的NSOperation对象 if ([[queue operations] count] > 0) op = [queue.operations objectAtIndex:0]; 但由于某些原因,它最终会出现以下异常:索引0超出空数组的边界 我理解错误消息,但是我很惊讶,因为我在请求对象本身之前检查了队列计数 有什么想法吗 该操作可能在两次调用之间完成该操作可能在两次调用之间

我最近犯了一个奇怪的错误。NSOperationQueue表示其中有1个对象,但我无法访问其中的NSOperation对象

    if ([[queue operations] count] > 0)
    op = [queue.operations objectAtIndex:0];
但由于某些原因,它最终会出现以下异常:索引0超出空数组的边界

我理解错误消息,但是我很惊讶,因为我在请求对象本身之前检查了队列计数


有什么想法吗

该操作可能在两次调用之间完成

该操作可能在两次调用之间完成

请记住,操作可以在单独的线程上运行,并且通常是。实际上,有自己的方法获取计数,称为
operationCount
,并提供以下警告:

此方法返回的值 反映的是 对象,并根据需要进行更改 操作已完成。因此 当您使用返回的 值,实际操作数 可能不同。因此,你应该 此值仅用于近似值 指导,而不应依赖它来实现 对象枚举或其他精确的 计算

您遇到的可能是并发问题。需要考虑的是复制操作数组。< /P>
NSArray *ops = [queue.operations copy];
if ([ops count] > 0)
{
    op = [ops objectAtIndex:0];
    //You can check if it has finished using [op isFinished];
    //and do what you need to do here
}
[ops release];
更新:

下面是一个例子,说明为什么你会经常看到这种情况

//Set up and start an NSOperation
...

//Here your call to operations probably put some sort of lock
//around operations to retrieve them but your operation also
//finished and is waiting for your lock to complete to remove
//the operation. The operations call probably returns a copy.
if([[que operations] count] > 0)
{
    //Now the operation queue can access its operations and remove
    //the item with the lock released (it can actually access as early
    //as before the call and count)

    //Uh oh now there are 0 operations
    op = [queue.operations objectAtIndex:0];

}

请记住,操作可以在单独的线程上运行,并且通常是。实际上,有自己的方法获取计数,称为
operationCount
,并提供以下警告:

此方法返回的值 反映的是 对象,并根据需要进行更改 操作已完成。因此 当您使用返回的 值,实际操作数 可能不同。因此,你应该 此值仅用于近似值 指导,而不应依赖它来实现 对象枚举或其他精确的 计算

您遇到的可能是并发问题。需要考虑的是复制操作数组。< /P>
NSArray *ops = [queue.operations copy];
if ([ops count] > 0)
{
    op = [ops objectAtIndex:0];
    //You can check if it has finished using [op isFinished];
    //and do what you need to do here
}
[ops release];
更新:

下面是一个例子,说明为什么你会经常看到这种情况

//Set up and start an NSOperation
...

//Here your call to operations probably put some sort of lock
//around operations to retrieve them but your operation also
//finished and is waiting for your lock to complete to remove
//the operation. The operations call probably returns a copy.
if([[que operations] count] > 0)
{
    //Now the operation queue can access its operations and remove
    //the item with the lock released (it can actually access as early
    //as before the call and count)

    //Uh oh now there are 0 operations
    op = [queue.operations objectAtIndex:0];

}

您的意思是,在检查计数和分配变量之间,操作完成的可能性很大?这种情况下很奇怪,因为op=赋值毫无意义,根本不能使用。是的,并发性很好,不是吗?这就是复制操作的原因,如果您真的想对第一个操作执行某些操作,请复制我的示例中的操作,然后您可以安全地使用
op
。如果您关心它是否完成,只需调用
isFinished
方法。我在我的代码中使用了一些苹果的例子,但似乎应该注意这些“复制”的东西。同时,我解决了这个问题,没有使用op=语句,因为没有真正的需要。再次感谢你,乔。你的意思是在检查计数和分配变量之间有很高的机会完成操作?这种情况下很奇怪,因为op=赋值毫无意义,根本不能使用。是的,并发性很好,不是吗?这就是复制操作的原因,如果您真的想对第一个操作执行某些操作,请复制我的示例中的操作,然后您可以安全地使用
op
。如果您关心它是否完成,只需调用
isFinished
方法。我在我的代码中使用了一些苹果的例子,但似乎应该注意这些“复制”的东西。同时,我解决了这个问题,没有使用op=语句,因为没有真正的需要。再次感谢你,乔。