同步 UIImageView 中的后台作业
我有一个这样定义的接口
@interface PageViewController : UIImageView {
...
}
-(void) setImageUsing:(id) obj;
@end
,并且实现类似于以下
-(void) setImageUsing:(id) obj
{
...
[self performSelectorInBackground:@selector(changeImage) withObject:nil];
}
setImageUsing
,作为用户与应用程序交互的结果,从其他代码调用。
我应该如何继续将 PageViewController
一次限制为仅一项后台作业?
换句话说,我想在对 setImageUsing
进行新调用时取消任何正在运行的后台作业,然后启动一个新的后台作业。
编辑/解决方案:
正如 Ishu 提议的那样,我尝试使用 NSLock 进行同步。最终的代码(解决了问题)如下所示:
@interface PageViewController : UIImageView {
...
NSLock* m_threadLock;
NSLock* m_externalLock;
BOOL m_cancelThread;
}
-(void) setImageUsing:(id) obj;
@end
实现如下所示
-(void) setImageUsing:(id) obj
{
[m_externalLock lock];
...
if ([m_threadLock tryLock])
{
// succeeded in locking thread lock. so there is no background job running.
// unlock the lock and continue
[m_threadLock unlock];
}
else
{
// failed to lock thread lock. so there IS a job already running.
// ask the job to cancel
m_cancelThread = YES;
}
[self performSelectorInBackground:@selector(changeImage) withObject:nil];
[m_externalLock unlock];
}
-(void) changeImage
{
[m_threadLock lock];
....
if (m_cancelThread)
{
// cancel
}
...
m_cancelThread = NO;
[m_threadLock unlock];
}
希望对其他人有帮助。
I have an interface defined like this
@interface PageViewController : UIImageView {
...
}
-(void) setImageUsing:(id) obj;
@end
and implementation is like the following
-(void) setImageUsing:(id) obj
{
...
[self performSelectorInBackground:@selector(changeImage) withObject:nil];
}
setImageUsing
is called from other code as result of user interactions with the application.
How should I proceed to limit PageViewController
to only one background job at a time?
In other words, I would like to cancel any running background job when a new call to setImageUsing
is made and start a new background job after that.
EDIT/SOLUTION:
As Ishu proposed, I tried to use NSLock for synchronization purposes. The final code (that solved the issue) looks like this:
@interface PageViewController : UIImageView {
...
NSLock* m_threadLock;
NSLock* m_externalLock;
BOOL m_cancelThread;
}
-(void) setImageUsing:(id) obj;
@end
and implementation is like the following
-(void) setImageUsing:(id) obj
{
[m_externalLock lock];
...
if ([m_threadLock tryLock])
{
// succeeded in locking thread lock. so there is no background job running.
// unlock the lock and continue
[m_threadLock unlock];
}
else
{
// failed to lock thread lock. so there IS a job already running.
// ask the job to cancel
m_cancelThread = YES;
}
[self performSelectorInBackground:@selector(changeImage) withObject:nil];
[m_externalLock unlock];
}
-(void) changeImage
{
[m_threadLock lock];
....
if (m_cancelThread)
{
// cancel
}
...
m_cancelThread = NO;
[m_threadLock unlock];
}
Hope it'll help someone else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是终止任何后台任务的好方法,竞争后台线程执行是很好的,请参阅此 线程。您需要的是,您需要锁定线程分配以防止某种意外发生。请参阅开发人员文档中的 NSLocks,一些谷歌搜索会为您提供一些有关 NSLock 的信息。
This is not good way to terminate any background task , competing the background thread execution is good see this thread. What you need , you need to lock an thread allocation for some kind of mishappenings. See NSLocks in developer documentation and some googling gives you some info about NSLock.