dispatch queue 类型
1:main queue
这个queue在main thread 上执行所以的task,比如那些在cocoa和cocoatouch ui 相关的方法。
dispatch_get_main_queue取得main queue
2:concurrent queue
这类queue 让你可以按顺序执行asynchronous 或者synchronous的task。多个并发queue在同一时刻可以同时执行多个task,不用进行线程管理。
dispatch_get_global_queue 取得一个concurrent queue
3:serial queue
这类queue,对你提交的synchronous 或者 asynchronous 的task 没有重要性区分,按照先进先出(fifo)的方式执行,这就意味这在同一时间只能执行一个block object。
这些task不是在main thread上运行,因此不会阻塞main thread。
dispatch_queue_create 创建一个 serial queue。dispatch_release 释放一个serial queue。
把task添加进queue 的方式
1:block
2:c function
如何创建一个block
返回值类型 block名称 参数
NSInteger (^subtract)(NSInteger,NSInteger) = ^(NSInteger paramValue,NSInteger paramFrom)
{
return paramFrom - paramValue;
}
NSString* (^intToString)(NSUInteger) = ^(NSUInteger paramInteger){
NSString *result = [NSString stringWithFormat:@"%lu",
(unsigned long)paramInteger];
return result;
};
typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);
IntToStringConverter inlineConverter = ^(NSUInteger paramInteger){
NSString *result = [NSString stringWithFormat:@"%lu",
(unsigned long)paramInteger];
return result;
};
block 可以被传递
- (NSString *) convertIntToString:(NSUInteger)paramInteger
usingBlockObject:(IntToStringConverter)paramBlockObject{
return paramBlockObject(paramInteger);
}
objective c 方法访问 variable 与 block 访问 variable之间的不同
• Local variables in block objects work exactly the same as in Objective-C methods.
局部变量的方式效果是一样的
• For inline block objects, local variables constitute not only variables defined within
the block, but also the variables that have been defined in the method that implements
that block object. (Examples will come shortly.)
对于内嵌block 对象,局部变量的构成不仅仅是由定义在block内部的变量构成
• You cannot refer to self in independent block objects implemented in an
Objective-C class. If you need to access self, you must pass that object to the block
object as a parameter. We will see an example of this soon.
你不可以引用self对象在独立的block object中,如果你需要访问self,你必须把self作为一个参数传递给block。
• You can refer to self in an inline block object only if self is present in the lexical
scope inside which the block object is created.
在内嵌block中你可以引用self,只要self在创建block object 的lexical scope内
• For inline block objects, local variables that are defined inside the block object’s
implementation can be read from and written to. In other words, the block object
has read-write access to variables defined inside the block object’s body.
内嵌block对声明在block内部的局部变量具有读写权限
• For inline block objects, variables local to the Objective-C method that implements
that block can only be read from, not written to. There is an exception, though: a
block object can write to such variables if they are defined with the __block storage
type. We will see an example of this as well.
对于内嵌block object ,在objective c 方法中的局部变量,block 对象只能read,不能written。除非改变量被声明为 _ _ block
• Suppose you have an object of type NSObject and inside that object’s implementation
you are using a block object in conjunction with GCD. Inside this block
object, you will have read-write access to declared properties of that NSObject inside
which your block is implemented.
• You can access declared properties of your NSObject inside independent block objects
only if you use the setter and getter methods of these properties. You cannot
access declared properties of an object using dot notation inside an independent
block object.