线程,优先级和抛弃 [英] Threading, priorities, and abandonment

查看:235
本文介绍了线程,优先级和抛弃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近成为线程好奇的iOS。请指点我的方向,在现代iOS设备上实现(如果可能的话)以下内容...谢谢!



用户正在输入文本,



有时我想启动DifficultProcess来做一些语义处理。总之,我想我需要能够做四件事:




  • 从主
  • 启动DifficultProcess
  • 如果DifficultProcess完成,从它返回到相同的主

  • 放弃,清除,困难处理,如果我想,从主

  • 最后的优先级问题:DifficultProcess必须有比主要或用户输入低得多的优先级,我想让DifficultProcess真正真正优先级;


  • 在现代(2011年)中,A,B,C使用的呼叫基本上是什么1月)iOS?我不在乎爸爸的方法!



    我猜这是四个想法!



    所以特别是我想发送一个消息,换句话说,调用一个例程,运行后台进程(以这种方式,如果需要,可以杀死运行的后台进程,或者可能更改它的操作模式等)



    (对于1997年之前出生的任何人,您都会认为这是典型的投机处理范例。)


    $

    解决方案

    我建议使用NSOperation和NSOperationQueue以管理您需要能够随意取消的后台活动。



    NSOperation的-cancel和NSOperationQueue的-cancelAllOperations是要查看的方法。



    背景到主线程,dispatch_async到主线程队列技术是好的。



    例如

    pre> @protocol MyOperationDelegate
    - (void)operationStarted:(MyOperation *)operation;
    - (void)makingProgressOnItem:(id)anItem otherInterestingItem:(NSDictionary *)otherItem remainingCount:(NSUInteger)count;
    - (void)operationWillFinish:(MyOperation *)operation;
    @end

    @interface MyOperation
    id< MyOperationDelegate>代表;
    @end

    @implementation MyOperation
    ...

    - (void)cancel
    {
    [super cancel] ;

    //告诉代表我们即将完成(由于取消)。
    dispatch_sync(dispatch_get_main_queue(),^ {
    [self.delegate operationWillFinish:self];
    });
    }

    - (void)main
    {
    //检查取消

    if(self.isCancelled)return;

    //开始

    dispatch_sync(dispatch_get_main_queue(),^ {
    [self.delegate operationStarted:self];
    });

    if(self.isCancelled)return; //另一个取消检查


    //在做某些工作时定期发送异步进度消息

    while(workNotDone)
    {
    / / do some work ...

    dispatch_async(dispatch_get_main_queue(),^ {
    [self.delegate makingProgressOnItem:foo otherInterestingItem:bar remainingCount:baz];
    }

    if(self.isCancelled)return;
    }


    //即将完成

    if(!self.isCancelled){
    dispatch_sync(dispatch_get_main_queue
    [self.delegate operationWillFinish:self];
    });
    }
    }
    @end






    KVO对于线程间通信没有好处;在发生键值变化的线程上接收到观察。所以,如果你的后台线程改变一个值,你的后台线程将收到关于它的KVO。可能不是你想要的。



    Grandpa的-performSelectorOnMainThread:withObject:waitUntilDone:继续是一个很好的方式来获取消息回主线程。限制是你的消息只能访问一个基于对象的参数。对主线程的dispatch_async没有这个限制。



    如果你想从一个后台线程触发一个异步(或同步)NSNotification到主线程,您需要使用-performSelectorOnMainThread。

      NSNotification * note = [NSNotification notificationWithName:FinishedABunchOfWorkNotification object:self userInfo:nil]; 
    [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification :) withObject:note waitUntilDone:YES];


    I have recently become thread curious on iOS. Please point me in the direction you would take, to achieve (if possible) the following on modern iOS devices... thank you!

    The user is typing in text, say a word every few seconds.

    From time to time I want to launch DifficultProcess to do some semantic processing. In short, I guess I need to be able to do four things:

    • launch DifficultProcess from main
    • if DifficultProcess completes, get a message back from it to the same main
    • abandon, get rid of, DifficultProcess if I want to, from main
    • and finally the priority question: DifficultProcess must have much lower priority than main or user input, I want DifficultProcess to have really really looow priority; is that even possible?

    What, essentially, are the calls one uses for A, B, C in modern (2011) (late January) iOS? I don't care about Dad's methods! And is "D" even possible in any way?

    I guess those are the four ideas!

    So in particular I want to send a message to, in other words call a routine in, the running background process (in that way, one could kill off the running background process if desired, or perhaps change it's mode of operation etc).

    (For anyone born before 1997, you will recognise that as a typical "speculative processing" paradigm.)

    Thanks for pointers for anyone who can be bothered on this!

    解决方案

    I would recommend using NSOperation and NSOperationQueue to manage background activity that you need to be able to cancel arbitrarily.

    NSOperation's -cancel and NSOperationQueue's -cancelAllOperations are the methods to look at.

    To get messages back from the background to the main thread, the dispatch_async-to-main-thread-queue technique is fine. You can combine this with a delegate protocol for your NSOperation to codify the messages you want to send back.

    E.g.

    @protocol MyOperationDelegate
    - (void) operationStarted:(MyOperation *)operation;
    - (void) makingProgressOnItem:(id)anItem otherInterestingItem:(NSDictionary *)otherItem remainingCount:(NSUInteger)count;
    - (void) operationWillFinish:(MyOperation *)operation;
    @end
    
    @interface MyOperation
    id <MyOperationDelegate> delegate;
    @end
    
    @implementation MyOperation
    ...
    
    - (void) cancel
    { 
        [super cancel];
    
        // Tell the delegate we're about to finish (due to cancellation).
        dispatch_sync (dispatch_get_main_queue(), ^{
          [self.delegate operationWillFinish:self];
        });
    }
    
    - (void) main 
    {
        // Check for cancellation
    
        if (self.isCancelled) return;
    
        // Starting
    
        dispatch_sync (dispatch_get_main_queue(), ^{
            [self.delegate operationStarted:self];
        });
    
        if (self.isCancelled) return; // Another cancel check
    
    
        // Send async progress messages periodically while doing some work
    
        while (workNotDone) 
        {
            // Do some work ...
    
            dispatch_async (dispatch_get_main_queue(), ^{
               [self.delegate makingProgressOnItem:foo otherInterestingItem:bar remainingCount:baz];
            });
    
           if (self.isCancelled) return;
        }
    
    
        // About to finish
    
        if (!self.isCancelled) {
            dispatch_sync (dispatch_get_main_queue(), ^{
               [self.delegate operationWillFinish:self];
            });
        }
    }
    @end
    


    KVO is no good for interthread communication; the observation is received on the thread that originates the key value change. So, if your background thread changes a value, your background thread is going to receive the KVO about it. Probably not what you want.

    Grandpa's -performSelectorOnMainThread:withObject:waitUntilDone: continues to be a fine way to get messages back to the main thread. The limitation is that your messages can only access one object-based argument. The dispatch_async to the main thread doesn't have this limitation.

    If you want to fire off an asynchronous (or synchronous) NSNotification's from a background thread to the main thread, you need to use -performSelectorOnMainThread.

    NSNotification *note = [NSNotification notificationWithName:FinishedABunchOfWorkNotification object:self userInfo:nil];
    [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:note waitUntilDone:YES];
    

    这篇关于线程,优先级和抛弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆