1

我开发了一个类别,使 NSOperation 能够在后台以定时间隔执行。我真的很感激能得到一些反馈,尤其是我没有想到的这种方法的任何潜在问题。

谢谢!

这是代码:

NSOperation+Repeat.h

#import <Foundation/Foundation.h>

@interface NSOperation (repeat)

@property (readonly, nonatomic) NSTimeInterval repeatInterval;
@property (readonly, nonatomic) NSOperationQueue *repeatOperationQueue;

- (void)performUsingOperationQueue:(NSOperationQueue *)operationQueue;
- (void)performAtRepeatingInterval:(NSTimeInterval)interval usingOperationQueue:(NSOperationQueue *)operationQueue;

@end

NSOperation+Repeat.m

#import "NSOperation+repeat.h"
#import <objc/runtime.h>

static char const * const RepeatPropertiesKey = "RepeatProperties";

@implementation NSOperation (repeat)

@dynamic repeatInterval;
@dynamic repeatOperationQueue;

static NSString * RepeatIntervalKey = @"interval";
static NSString * RepeatOperationQueueKey = @"operationQueue";
static NSString * RepeatTimerKey = @"timer";

- (NSMutableDictionary *)repeatProperties {
    NSMutableDictionary * properties = objc_getAssociatedObject(self, RepeatPropertiesKey);
    if (properties == nil) {
        properties = [NSMutableDictionary new];
        objc_setAssociatedObject(self, RepeatPropertiesKey, properties, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return properties;
}

- (NSTimeInterval)interval {
    NSNumber * interval = [[self repeatProperties] objectForKey:RepeatIntervalKey];
    return [interval doubleValue];
}

- (NSOperationQueue *)repeatOperationQueue {
    NSOperationQueue * operationQueue = [[self repeatProperties] objectForKey:RepeatOperationQueueKey];
    return operationQueue;
}

- (void)performUsingOperationQueue:(NSOperationQueue *)operationQueue {
    [operationQueue addOperation:[self copy]];
}

- (void)performAtInterval:(NSTimer *)timer {
    [self performUsingOperationQueue:self.repeatOperationQueue];
}

- (void)performAtRepeatingInterval:(NSTimeInterval)interval usingOperationQueue:(NSOperationQueue *)operationQueue {
    // Save interval and operationQueue in repeatProperties
    [self.repeatProperties setValue:[NSNumber numberWithDouble:interval] forKey:RepeatIntervalKey];
    [self.repeatProperties setValue:operationQueue forKey:RepeatOperationQueueKey];

    // Create timer to call performAtInterval on self
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(interval*60)
                                                      target:self
                                                    selector:@selector(performAtInterval:)
                                                    userInfo:nil
                                                     repeats:YES];

    // Save the timer in repeatProperties
    [self.repeatProperties setValue:timer forKey:RepeatTimerKey];

    [self performUsingOperationQueue:operationQueue];
}

@end

这是一个可以重复的 NSOperation 子类的示例:

MSScheduleImportOperation.h

#import <Foundation/Foundation.h>
#import "NSOperation+Repeat.h"

@interface MSScheduleImportOperation : NSOperation <NSCopying>

@property (readonly, strong, nonatomic) NSString* employeeId;

- (id)initWithEmployeeId:(NSString *)employeeId;

@end

MSScheduleImportOperation.m

#import "MSScheduleImportOperation.h"

@implementation MSScheduleImportOperation

@synthesize employeeId = __employeeId;

- (id)initWithEmployeeId:(NSString *)employeeId     {
    self = [super init];
    __employeeId = [employeeId copy];
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    MSScheduleImportOperation* copy = [[MSScheduleImportOperation alloc] initWithEmployeeId:self.employeeId];
    return copy;
}

- (void)main
{
 ...   
}


@end
4

2 回答 2

2

苹果文档说:

一个操作对象是一个单次对象——也就是说,它执行一次它的任务,不能被用来再次执行它。

所以第一个问题是可能有内部因素阻止它工作。虽然,我看到你试图通过复制来解决这个问题。

这给我们带来了另一个问题,NSOperation就是没有标榜符合NSCopying

[operationQueue addOperation:[self copy]];

此行应引发异常。

于 2012-05-10T15:03:02.557 回答
1

NSOperation而不是对象复制自身并将副本添加到的类别NSOperationQueue- 在更高级别管理它会更简单。例如:

+[RepeatingOperation operationBlock:(InitOperationBlock)operationBlock
                              queue:(NSOperationQueue*)queue
                           interval:(NSTimeInterval)interval];

whereInitOperationBlock将是创建和配置操作的块。

主要的好处是 API 更难搞砸。例如在原始帖子中的类别中,performUsingOperationQueue:如果您忘记设置repeatOperationQueue.

于 2015-11-05T21:51:47.480 回答