循环 CFRunLoopSource

发布于 2024-09-18 03:20:56 字数 3990 浏览 2 评论 0原文

这是我目前正在开发的源文件。这个类背后的想法是包含一个目标对象 和选择器,它将在传递给 ScheduleInCFRunLoop 的任何 CFRunLoop 中调用。我需要它重复循环,而不消耗 iPhone 上的所有可用处理时间。完成本课程时的任何帮助都会非常有帮助,我花了一整天的时间上网尝试查找有关自定义 CFRunLoop 源的有用信息,但我尚未找到任何对完成本课程有用的信息。

RunLoopContext.h

//
//  RunLoopContext.h
//  ETAClient-iPhoneOS
//
//  Created by Daniel Reed on 9/7/10.
//  Copyright 2010 N/a. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface RunLoopContext : NSObject 
{
  id target;
  SEL selector;

  CFRunLoopSourceContext context;
  CFRunLoopSourceRef source;
  CFRunLoopRef runLoop;
  CFStringRef mode;
}

#pragma mark -
#pragma mark Property directives
@property (assign) id target;
@property (assign) SEL selector;
@property (readonly) CFRunLoopSourceContext context;
@property (readonly) CFRunLoopSourceRef source;
@property (readonly) CFRunLoopRef runLoop;

#pragma mark -
#pragma mark Custom initializers
-(id) initWithTarget: (id) aTarget selector: (SEL) aSelector;

#pragma mark -
#pragma mark Public methods
-(BOOL) isValid;
-(void) invalidate;
-(void) signal;
-(void) invoke;
-(void) scheduleInCFRunLoop: (CFRunLoopRef) aRunLoop forMode: (CFStringRef) mode;
@end

#pragma mark -
#pragma mark CFRunLoopSourceContext callbacks
void RunLoopSourceScheduleRoutine (void *info, CFRunLoopRef rl, CFStringRef mode);
void RunLoopSourcePerformRoutine (void *info);
void RunLoopSourceCancelRoutine (void *info, CFRunLoopRef rl, CFStringRef mode);

RunLoopContext.m

//
//  RunLoopContext.m
//  ETAClient-iPhoneOS
//
//  Created by Daniel Reed on 9/7/10.
//  Copyright 2010 N/a. All rights reserved.
//

#import "RunLoopContext.h"


@implementation RunLoopContext
#pragma mark -
#pragma mark Synthesize directives
@synthesize target;
@synthesize selector;
@synthesize context;
@synthesize source;
@synthesize runLoop;

#pragma mark -
#pragma mark Custom initializers
-(id) initWithTarget: (id) aTarget selector: (SEL) aSelector
{
  if (self = [super init])
  {
    target = aTarget;
    selector = aSelector;
  }

  return self;
}

#pragma mark -
#pragma mark Public methods
-(BOOL) isValid
{
  return CFRunLoopSourceIsValid(source);
}

-(void) invalidate
{
  CFRunLoopSourceInvalidate(source);
}

-(void) signal
{
  CFRunLoopSourceSignal(source);
  CFRunLoopWakeUp(runLoop);
}

-(void) invoke
{
  // Perform the target selector.
  [target performSelector: selector];
}

-(void) scheduleInCFRunLoop: (CFRunLoopRef) aRunLoop forMode: (CFStringRef) aMode
{
  // Setup the context.
  context.version = 0;
  context.info = self;
  context.retain = NULL;
  context.release = NULL;
  context.copyDescription = CFCopyDescription;
  context.equal = CFEqual;
  context.hash = CFHash;
  context.schedule = RunLoopSourceScheduleRoutine;
  context.cancel = RunLoopSourceCancelRoutine;
  context.perform = RunLoopSourcePerformRoutine;

  // Store the configured runloop and mode.
  runLoop = aRunLoop;
  mode = aMode;

  // Create the CFRunLoopSourceRef.
  source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);

  // Add the new CFRunLoopSourceRef to the indicated runloop.
  CFRunLoopAddSource(runLoop, source, mode);
}

#pragma mark -
#pragma mark Overriden inherited methods
-(void) dealloc
{
  // Invalidate.
  [self invalidate];

  // Set retained objects/values to nil.
  target = nil;
  selector = nil;

  // Invoke the inherited dealloc method.
  [super dealloc];
}
@end

#pragma mark -
#pragma mark CFRunLoopSourceContext callbacks
void RunLoopSourceScheduleRoutine(void* info, CFRunLoopRef rl, CFStringRef mode)
{
  // Cast the info pointer to a RunLoopContext instance.
  RunLoopContext* ctx = (RunLoopContext*) info;
}

void RunLoopSourcePerformRoutine (void* info)
{
  // Cast the info pointer to a RunLoopContext instance.
  RunLoopContext* ctx = (RunLoopContext*) info;

  if (ctx)
  {
    [ctx invoke];
  }
}

void RunLoopSourceCancelRoutine (void* info, CFRunLoopRef rl, CFStringRef mode)
{

}

Here is the source files I'm currently developing. The idea behind this class is to contain a target object
and selector which will be invoked in whatever CFRunLoop passed to scheduleInCFRunLoop. I need this to loop repeatedly without consuming all available processing time on the iPhone. Any help on completion will be most helpful, I've spent an entire day surfing the internet trying to find helpful information regarding custom CFRunLoop sources and I am yet to find anything useful for completing this class.

RunLoopContext.h

//
//  RunLoopContext.h
//  ETAClient-iPhoneOS
//
//  Created by Daniel Reed on 9/7/10.
//  Copyright 2010 N/a. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface RunLoopContext : NSObject 
{
  id target;
  SEL selector;

  CFRunLoopSourceContext context;
  CFRunLoopSourceRef source;
  CFRunLoopRef runLoop;
  CFStringRef mode;
}

#pragma mark -
#pragma mark Property directives
@property (assign) id target;
@property (assign) SEL selector;
@property (readonly) CFRunLoopSourceContext context;
@property (readonly) CFRunLoopSourceRef source;
@property (readonly) CFRunLoopRef runLoop;

#pragma mark -
#pragma mark Custom initializers
-(id) initWithTarget: (id) aTarget selector: (SEL) aSelector;

#pragma mark -
#pragma mark Public methods
-(BOOL) isValid;
-(void) invalidate;
-(void) signal;
-(void) invoke;
-(void) scheduleInCFRunLoop: (CFRunLoopRef) aRunLoop forMode: (CFStringRef) mode;
@end

#pragma mark -
#pragma mark CFRunLoopSourceContext callbacks
void RunLoopSourceScheduleRoutine (void *info, CFRunLoopRef rl, CFStringRef mode);
void RunLoopSourcePerformRoutine (void *info);
void RunLoopSourceCancelRoutine (void *info, CFRunLoopRef rl, CFStringRef mode);

RunLoopContext.m

//
//  RunLoopContext.m
//  ETAClient-iPhoneOS
//
//  Created by Daniel Reed on 9/7/10.
//  Copyright 2010 N/a. All rights reserved.
//

#import "RunLoopContext.h"


@implementation RunLoopContext
#pragma mark -
#pragma mark Synthesize directives
@synthesize target;
@synthesize selector;
@synthesize context;
@synthesize source;
@synthesize runLoop;

#pragma mark -
#pragma mark Custom initializers
-(id) initWithTarget: (id) aTarget selector: (SEL) aSelector
{
  if (self = [super init])
  {
    target = aTarget;
    selector = aSelector;
  }

  return self;
}

#pragma mark -
#pragma mark Public methods
-(BOOL) isValid
{
  return CFRunLoopSourceIsValid(source);
}

-(void) invalidate
{
  CFRunLoopSourceInvalidate(source);
}

-(void) signal
{
  CFRunLoopSourceSignal(source);
  CFRunLoopWakeUp(runLoop);
}

-(void) invoke
{
  // Perform the target selector.
  [target performSelector: selector];
}

-(void) scheduleInCFRunLoop: (CFRunLoopRef) aRunLoop forMode: (CFStringRef) aMode
{
  // Setup the context.
  context.version = 0;
  context.info = self;
  context.retain = NULL;
  context.release = NULL;
  context.copyDescription = CFCopyDescription;
  context.equal = CFEqual;
  context.hash = CFHash;
  context.schedule = RunLoopSourceScheduleRoutine;
  context.cancel = RunLoopSourceCancelRoutine;
  context.perform = RunLoopSourcePerformRoutine;

  // Store the configured runloop and mode.
  runLoop = aRunLoop;
  mode = aMode;

  // Create the CFRunLoopSourceRef.
  source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);

  // Add the new CFRunLoopSourceRef to the indicated runloop.
  CFRunLoopAddSource(runLoop, source, mode);
}

#pragma mark -
#pragma mark Overriden inherited methods
-(void) dealloc
{
  // Invalidate.
  [self invalidate];

  // Set retained objects/values to nil.
  target = nil;
  selector = nil;

  // Invoke the inherited dealloc method.
  [super dealloc];
}
@end

#pragma mark -
#pragma mark CFRunLoopSourceContext callbacks
void RunLoopSourceScheduleRoutine(void* info, CFRunLoopRef rl, CFStringRef mode)
{
  // Cast the info pointer to a RunLoopContext instance.
  RunLoopContext* ctx = (RunLoopContext*) info;
}

void RunLoopSourcePerformRoutine (void* info)
{
  // Cast the info pointer to a RunLoopContext instance.
  RunLoopContext* ctx = (RunLoopContext*) info;

  if (ctx)
  {
    [ctx invoke];
  }
}

void RunLoopSourceCancelRoutine (void* info, CFRunLoopRef rl, CFStringRef mode)
{

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最笨的告白 2024-09-25 03:20:56

在我看来,您正在重新实现 NSTimer。你有不能使用该类的原因吗?它基本上做同样的事情。

Looks to me like you are re-implementing NSTimer. Is there a reason why you cannot use that class? It basically does the same thing.

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