iOS并发编程:操作队列、依赖关系与定时器的运用
发布时间: 2025-08-17 01:56:34 阅读量: 2 订阅数: 11 

### iOS并发编程:操作队列、依赖关系与定时器的运用
在iOS开发中,实现并发操作、创建操作之间的依赖关系以及使用定时器是常见的需求。下面将详细介绍如何使用操作队列异步执行任务、创建操作之间的依赖关系以及创建定时器。
#### 1. 异步执行任务
当需要并发执行操作时,有两种主要的解决方案:使用操作队列或子类化`NSOperation`并在`main`方法中分离新线程。
##### 1.1 使用操作队列
操作队列是管理并发操作的有效方式。以下是一个使用操作队列和两个简单调用操作的示例:
```objc
// 声明文件(.h)
#import <UIKit/UIKit.h>
@interface Running_Tasks_Asynchronously_with_OperationsAppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@property (nonatomic, strong) NSInvocationOperation *firstOperation;
@property (nonatomic, strong) NSInvocationOperation *secondOperation;
@end
// 实现文件(.m)
#import "Running_Tasks_Asynchronously_with_OperationsAppDelegate.h"
@implementation Running_Tasks_Asynchronously_with_OperationsAppDelegate
- (void) firstOperationEntry:(id)paramObject{
NSLog(@"%s", __FUNCTION__);
NSLog(@"Parameter Object = %@", paramObject);
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
}
- (void) secondOperationEntry:(id)paramObject{
NSLog(@"%s", __FUNCTION__);
NSLog(@"Parameter Object = %@", paramObject);
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSNumber *firstNumber = [NSNumber numberWithInteger:111];
NSNumber *secondNumber = [NSNumber numberWithInteger:222];
self.firstOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(firstOperationEntry:) object:firstNumber];
self.secondOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(secondOperationEntry:) object:secondNumber];
self.operationQueue = [[NSOperationQueue alloc] init];
[self.operationQueue addOperation:self.firstOperation];
[self.operationQueue addOperation:self.secondOperation];
NSLog(@"Main thread is here");
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
```
上述代码的执行步骤如下:
1. 定义两个方法`firstOperationEntry:`和`secondOperationEntry:`,用于打印当前线程、主线程和参数信息。
2. 初始化两个`NSInvocationOperation`对象,并设置目标选择器。
3. 初始化一个`NSOperationQueue`对象,用于管理操作的并发执行。
4. 将两个调用操作添加到操作队列中。
运行代码后,会发现调用操作在各自的线程上并行运行,不会阻塞主线程。
##### 1.2 子类化`NSOperation`
如果需要更精细地控制操作,可以子类化`NSOperation`。以下是一个简单的示例:
```objc
// 声明文件(.h)
#import <Foundation/Foundation.h>
@interface SimpleOperation : NSOperation
- (id) initWithObject:(NSObject *)paramObject;
@end
// 实现文件(.m)
#import "SimpleOperation.h"
@implementation SimpleOperation
NSObject *givenObject;
BOOL finished;
BOOL executing;
- (id) init {
NSNumber *dummyObject = [NSNumber numberWithInteger:123];
return([self initWithObject:dummyObject]);
}
- (id) initWithObject:(NSObject *)paramObject{
self = [super init];
if (self != nil){
givenObject = paramObject;
}
return(self);
}
- (void) start {
if ([self isCancelled]){
[self willChangeValueForKey:@"isFinished"];
finished = YES;
[self didChangeValueForKey:@"isFinished"];
return;
} else {
[self willChangeValueForKey:@"isExecuting"];
executing = YES;
[self didChangeValueForKey:@"isExecuting"];
[self main];
}
}
- (void) main {
@try {
@autoreleasepool {
BOOL taskIsFinished = NO;
while (taskIsFinished == NO && [self isCancelled] == NO){
NSLog(@"%s", __FUNCTION__);
NSLog(@"Parameter Object = %@", givenObject);
NSLog(@"Main Thread = %@", [NSThread mainThread]);
NSLog(@"Current Thread = %@", [NSThread currentThread]);
taskIsFinished = YES;
}
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
finished = YES;
executing = NO;
[self didChangeValueForKey:@"isFinished"];
[self didChangeValueForKey:@"isExecuting"];
}
}
@catch (NSException * e) {
NSLog(@"Exception %@", e);
}
}
- (BOOL) isConcurrent{
return YES;
}
- (BOOL) isFinished{
return finished;
}
- (BOOL) isExecuting{
return executing;
}
@end
```
使用这个自定义操作类的示例代码如下:
```objc
// 声明文件(.h)
#import <UIKit/UIKit.h>
@class
```
0
0
相关推荐










