开发者

NSInvocationOperation Question

开发者 https://www.devze.com 2023-01-21 12:44 出处:网络
I would like to know how to remove duplicate nsoperations, i.e is there a way to check the nsoperation queue and see if I am making a duplicate request?

I would like to know how to remove duplicate nsoperations, i.e is there a way to check the nsoperation queue and see if I am making a duplicate request?

Basically, I am requesting 开发者_如何转开发images depending on the iphone screen rotation. When the view is loaded the shouldautorate is called twice.

If(rotation==portrait){
  request portrait image.
}

The issue is that shoud autorotate will be checked twice. And on both ocassions, it will turn out to be portrait hence requesting the same image twice. Anyone with a good idea? I forgot to mention I am using a queue.

Thanks.


NEW, better answer:

Don't use the shouldAutorotate... method. It only asks whether a rotation is allowed to happen. This may happen any time and may not necessarily lead to a rotation. Instead, use willRotate... or didRotate..., as these methods are only invoked exactly once and only if the rotation actually happens.

Your operation will then be added only once.


OLD, but not false answer:

How about using a instance variable to remember the last seen rotation? Like so:

if (rotation != lastSeenRotation) {
    // REQUEST image here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消