同步 UIImageView 中的后台作业

发布于 2024-11-17 18:33:54 字数 1690 浏览 3 评论 0原文

我有一个这样定义的接口

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-11-24 18:33:54

这不是终止任何后台任务的好方法,竞争后台线程执行是很好的,请参阅此 线程。您需要的是,您需要锁定线程分配以防止某种意外发生。请参阅开发人员文档中的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文