Chinaunix首页 | 论坛 | 博客
  • 博客访问: 429481
  • 博文数量: 83
  • 博客积分: 2622
  • 博客等级: 少校
  • 技术积分: 1345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 08:59
个人简介

一直在努力

文章分类

全部博文(83)

文章存档

2014年(3)

2013年(9)

2012年(46)

2010年(25)

分类:

2010-09-08 19:24:28

因为做翻译太浪费时间了,只好自己做下总结和重点:
CFRunloop是一种用于监视任务来源,并当这些任务变得可处理的时候选择正确调度的方法。来源可能是:用户输入设备,网络连接,定期或者固定延时,和异步回调。
1。三种可以使用run loop的对象是:sources timers observers,相关文档:CFRunLoopSource Reference  CFRunLoopTimer Reference  CFRunLoopObserver Reference
2。向run loop中添加object的方法 CFRunLoopAddSource,CFRunLoopAddTimer, or CFRunLoopAddObserver
3。移除方法,移出 run loop或者使之invalidate
4。run loops的运行模式:每个run loop都有不同的运行模式,默认的是kCFRunLoopDefaultMode以某种模式运行的run loop都包含有一系列自己的对象集合,默认的模式是当application或者thread处于idle状态是将会监控objects。其他的模式自动创建当一个object添加到一个 an unrecognized mode。每个run loop都有自己独立的模式集合。

???请问上述是么意思,头痛中~~~~

ps:他的意思似乎是说可以自己定义mode字符串,然后创建在一个runloop中的对象结合
其中提到kCFRunloopCommonModes这个伪模式,但是这个模式字符串不能用于 CFRunLoopRunInMode函数。它类似一个容器可以将多个mode添加到common mode中使用CFRunLoopAddCommonMode
ps2:每个run loop都有自己相互独立的一系列common modes。当run loop以固定模式运行是相应注册的对象才能运行,
ps3:一旦一个mode加入到common modes,就不能被移除。
ps4:The Add, Contains, and Remove functions for sources, timers, and observers operate on a run loop’s set of common modes when you use the constant kCFRunLoopCommonModes for the run loop mode.
补台理解:貌似是当run loop以kCFRunLoopCommonModes模式运行时,所有的对象操作都是kCFRunLoopCommonModes模式提供的add contains remove 方法。
ps5:默认的模式默认属于kCFRunLoopCommonModes

5。CFRunLoopRunInMode Exit Codes有四种
enum {
   kCFRunLoopRunFinished = 1,	//没有sources或者timers需要处理
   kCFRunLoopRunStopped = 2,	//CFRunloopStop在run loop中调用
   kCFRunLoopRunTimedOut = 3,	//超时终止,固定的时间间隔已经过去了
   kCFRunLoopRunHandledSource = 4//sources已经被处理了,这个值返回的情况仅仅是当前run loop用于处理这个返回值只有当运行循环被告知只能运行到一个源进行处理。
};

6。Managing Sources
CFRunLoopSourceRef CFRunLoopSourceCreate (
   CFAllocatorRef allocator,
   CFIndex order,
   CFRunLoopSourceContext *context
);
order是优先级索引,对version1无效,一般情况为0;

CFRunLoopSource’s behavior.

struct CFRunLoopSourceContext {
   CFIndex version; 0
   void *info;
   CFAllocatorRetainCallBack retain;
   CFAllocatorReleaseCallBack release;
   CFAllocatorCopyDescriptionCallBack copyDescription;
   CFRunLoopEqualCallBack equal;
   CFRunLoopHashCallBack hash;
   CFRunLoopScheduleCallBack schedule;
   CFRunLoopCancelCallBack cancel;
   CFRunLoopPerformCallBack perform;
};

CFRunLoopSourceContext1

A structure that contains program-defined data and callbacks with which you can configure a version 1 CFRunLoopSource’s behavior.

struct CFRunLoopSourceContext1 {
   CFIndex version; 1
   void *info;
   CFAllocatorRetainCallBack retain;
   CFAllocatorReleaseCallBack release;
   CFAllocatorCopyDescriptionCallBack copyDescription;
   CFRunLoopEqualCallBack equal;
   CFRunLoopHashCallBack hash;
   CFRunLoopGetPortCallBack getPort;
   CFRunLoopMachPerformCallBack perform;
};
7。Managing Observers
  • CFRunLoopAddObserver
  • CFRunLoopContainsObserver
  • CFRunLoopRemoveObserver
  • Each run loop observer can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop.

    Creates a CFRunLoopObserver object.

    CFRunLoopObserverRef CFRunLoopObserverCreate (
       CFAllocatorRef allocator,
       CFOptionFlags activities,
       Boolean repeats,
       CFIndex order,
       CFRunLoopObserverCallBack callout,
       CFRunLoopObserverContext *context
    );
    activities

    Set of flags identifying the activity stages of the run loop during which the observer should be called. See Run Loop Activitiesfor the list of stages. To have the observer called at multiple stages in the run loop, combine theRun Loop Activities values using the bitwise-OR operator.

    repeats

    A flag identifying whether the observer should be called only once or every time through the run loop. If repeatsis false, the observer is invalidated after it is called once, even if the observer was scheduled to be called at multiple stages within the run loop.

    struct CFRunLoopObserverContext {
       CFIndex version;
       void *info;
       CFAllocatorRetainCallBack retain;
       CFAllocatorReleaseCallBack release;
       CFAllocatorCopyDescriptionCallBack copyDescription;
    };
    typedef struct CFRunLoopObserverContext CFRunLoopObserverContext;
    enum CFRunLoopActivity {
       kCFRunLoopEntry = (1 << 0),
       kCFRunLoopBeforeTimers = (1 << 1),
       kCFRunLoopBeforeSources = (1 << 2),
       kCFRunLoopBeforeWaiting = (1 << 5),
       kCFRunLoopAfterWaiting = (1 << 6),
       kCFRunLoopExit = (1 << 7),
       kCFRunLoopAllActivities = 0x0FFFFFFFU
    };
    typedef enum CFRunLoopActivity CFRunLoopActivity;
  • 8。Managing Timers
    • CFRunLoopAddTimer
    • CFRunLoopGetNextTimerFireDate
    • CFRunLoopRemoveTimer
    • CFRunLoopContainsTimer
    • 9。A CFRunLoopObserver provides a general means to receive callbacks at different points within a running run loop. In contrast to sources, which fire when an asynchronous event occurs, and timers, which fire when a particular time passes, observers fire at special locations within the execution of the run loop, such as before sources are processed or before the run loop goes to sleep, waiting for an event to occur. Observers can be either one-time events or repeated every time through the run loop’s loop.
    • observer是监控run loop运行期间的某个特殊的时间点儿或者事件发生的触发回调函数,sources是等待的资源到位之后触发,timer是在特定时间段结束触发。
    • observer可以是在sources处理前,sleep前,
    • Each run loop observer can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop.
    • 更多信息参考相关文档
    • 所谓get 原则就是只CFRetain保留所有权的那一方
阅读(4483) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~