iOS: 学习笔记, performSelectorOnMainThread

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

performSelectorOnMainThread:withObject:waitUntilDone:

基于默认模式调用主线程中接收器的方法

Invokes a method of the receiver on the main thread using the default mode.

参数 Parameters

aSelector

一个选择器, 指定要调用的方法。该方法不应该有明确的返回值并且最多只有一个id类型的参数,或者没有参数。

A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.

arg

需要传递给调用方法的参数. 如果没有需要的参数,传入nil

The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.

wait

一个布尔值,指定是否阻塞当前线程直到指定选择器在主线程中执行完毕。选择YES会阻塞这个线程;选择NO,本方法会立刻返回。

A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the main thread. Specify YES to block this thread; otherwise, specify NO to have this method return immediately.

如果当前线程也是主线程,选择YES,消息会立即派发,处理。

If the current thread is also the main thread, and you specify YES for this parameter, the message is delivered and processed immediately.

讨论 Discussion

你可以使用本方法向主线程发送消息. 主线程包含应用程序的主循环(这是NSApplication接收事件的地方). 在这种情况下, 消息相当于是你想要在线程上执行的当前对象的方法.

You can use this method to deliver messages to the main thread of your application. The main thread encompasses the application’s main run loop, and is where the NSApplication object receives events. The message in this case is a method of the current object
that you want to execute on the thread.

本方法使用Common模式将消息加入到主线程运行循环(run loop)队列, 即 与NSRunLoopCommonModes常数相关的模式。作为其正常运行循环处理的一部分,主线程从队列中取出消息(假设它是在Common模式中运行)并调用所需的方法。在同一个线程多次调用此方法会导致相应的选择进行排队,再按相同的顺序取出执行。

This method queues the message on the run loop of the main thread using the common run loop modes—that is, the modes associated with the NSRunLoopCommonModes constant. As part of its normal run loop processing, the main thread dequeues the message (assuming
it is running in one of the common run loop modes) and invokes the desired method. Multiple calls to this method from the same thread cause the corresponding selectors to be queued and performed in the same same order in which the calls were made.

你不能取消由本方法加入队列的消息. 如果你想取消当前线程的一条消息, 必须使用

performSelector:withObject:afterDelay: 或

performSelector:withObject:afterDelay:inModes: 方法.

You cannot cancel messages queued using this method. If you want the option of canceling a message on the current thread, you must use either the

performSelector:withObject:afterDelay: or

performSelector:withObject:afterDelay:inModes: method.

特别注意事项

Special Considerations

本方法在runloop中注册当前上下文, 并且依赖于runloop定期运行以保证正确执行. (?????此处翻译不出, 请大家帮助)。如果你在运行一个调度队列时需要这种类型的功能, 你应该使用dispatch_after和相关的方法来得到你想要的行为。

This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly. One common context where you might call this method and end up registering with
a runloop that is not automatically run on a regular basis is when being invoked by a dispatch queue
. If you need this type of functionality when running on a dispatch queue, you should use dispatch_after and related methods to get the behavior
you want.

iOS: 学习笔记, performSelectorOnMainThread

时间: 2024-07-28 23:23:12

iOS: 学习笔记, performSelectorOnMainThread的相关文章

iOS: 学习笔记, 使用performSelectorOnMainThread及时刷新UIImageView

在iOS中, 界面刷新在主线程中进行, 这导致NSURLSession远程下载图片使用UIImageView直接设置Image并不能及时刷新界面. 下面的代码演示了如何使用 performSelectorOnMainThread: withObject:  waitUntilDone: 方法来及时刷新图片 1. 创建iOS空应用程序(Empty Application). 2. 加入一个控制器类. 在YYAppDelegate.m中修改 #import "MainViewController.h

iOS学习笔记-精华整理

iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始等待用户的操作,自动释放池就会被释放掉(调用dealloc),池中的对象都会收到一个release,有可能会因此被销毁. 2-成员属性:     readonly:不指定readonly,默认合成getter和setter方法.外界毫不关心的成员,则不要设置任何属性,这样封装能增加代码的独立性和安全

IOS学习笔记 -- 多线程

多线程1.多线程的原理 1>.同一时间,CPU只能处理1条线程,只有1条线程在工作(执行) 2>.多线程并发(同时)执行,其实是CPU快速地在多条线程之间调度(切换) 3>.如果CPU调度线程的时间足够快,就造成了多线程并发执行的假象如果线程非常非常多,会发生: 1>.CPU会在N多线程之间调度,CPU会累死,消耗大量的CPU资源 2>.每条线程被调度执行的频次会降低(线程的执行效率降低) 2.多线程的优缺点 1>.多线程的优点 能适当提高程序的执行效率 能适当提高资源

iOS学习笔记之UITableViewController&UITableView

iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论文,因此需要继续思考新的算法.这是一件挺痛苦的事情,特别是在很难找到与自己研究方向相关的文献的时候.也许网格序列水印这个课题本身的研究意义就是有待考证的.尽管如此,还是要努力的思考下去.由于实验室的原因,iOS的学习进度明显受到影响,加之整理文档本身是一件耗费时间和精力的事情,因此才这么久没有写笔记了. M

iOS: 学习笔记, Swift操作符定义

Swift操作符可以自行定义, 只需要加上简单的标志符即可. @infix 中置运算. 如+,-,*,/运算 @prefix 前置运算. 如- @postfix 后置运算. a++, a-- @assignment 赋值运算. +=, -=, --a, ++a // // main.swift // SwiftBasic // // Created by yao_yu on 14-7-27. // Copyright (c) 2014年 yao_yu. All rights reserved.

IOS学习笔记 -- Modal和Quartz2D

一. Modal1.Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止;Modal只是改变了View的现实,没有改变rootViewController 2.常用方法1>.以Modal的形式展示控制器- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion2>.关

iOS: 学习笔记, 添加一个带界面约束的控制器

1. 创建一个空iOS应用程序(Empty Application). 2. 添加加控制器类. 修改控制器类的viewDidLoad 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 //创建标题 5 UILabel *header = [[UILabel alloc] init]; 6 header.text = @"欢迎来到我的世界!"; 7 header.textAlignment = NSTextAlignmentCenter

iOS学习笔记(2)— UIView用户事件响应

iOS学习笔记(2)— UIView用户事件响应 UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相关的属性和方法. 1.交互相关的属性 userInteractionEnabled 默认是YES ,如果设置为NO则不响应用户事件,并且把当前控件从事件队列中删除.也就是说设置了userInterfaceEnabled属性的视图会打断响应者链导致该view的subview都无法响应事件. multipleTouchEnabled  默认是NO,如果设置为YE

iOS学习笔记(3)— 屏幕旋转

iOS学习笔记(3)— 屏幕旋转 一.屏幕旋转机制: iOS通过加速计判断当前的设备方向和屏幕旋转.当加速计检测到方向变化的时候,屏幕旋转的流程如下: 1.设备旋转时,系统接收到旋转事件. 2.系统将旋转事件通过AppDelegate通知当前的主Window. 3.window通知它的rootViewController. 4.rootViewController判断所支持的旋转方向,完成旋转. iOS系统中屏幕旋转事件没有像触碰事件那样进行hitTest,所以只有rootViewControl