10.2 NSOperation/NSOperationQueue:提供了一些在GCD中不容易实现的特性,如:限制最大并发数量,操作之间的依赖关系.

本文介绍了一个使用NSOperationQueue实现的任务队列案例,演示了如何通过自定义NSOperation子类来执行后台任务,并更新UI进度。此外,还展示了如何通过设置队列属性实现任务的并发控制及依赖关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<pre name="code" class="objc">//MyOperation.h

#import <Foundation/Foundation.h>

@interface MyOperation : NSOperation
@property(copy,atomic)NSString*name;
@end

#import "MyOperation.h"

@implementation MyOperation
-(void)main
{
    
    NSLog(@" %@ 线程开始执行任务",self.name);
 NSLog(@"----------------");
    for (int i=0; i<20; i++) {
        NSLog(@"    %d    ",i);
    }
    NSLog(@"%@任务执行完毕",self.name);
}
@end


//  ProgressOperation.h
//  10  OperationQueue
//
//  Created by Tracy on 15/5/29.
//  Copyright (c) 2015年 Tracy. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ViewController.h"
@interface ProgressOperation : NSOperation
@property(strong)ViewController *delegate;
@end

// <span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">//</span><span style="white-space: pre;">ViewController.h</span>
//  ProgressOperation.m
//  10  OperationQueue
//
//  Created by Tracy on 15/5/29.
//  Copyright (c) 2015年 Tracy. All rights reserved.
//


#import "ProgressOperation.h"


@implementation ProgressOperation
-(void)main
{
    for (int i=0; i<50; i++) {


//    主线程中更新UI
    [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
        }
}
-(void)updateUI
{
           [NSThread sleepForTimeInterval:0.02f];
    self.delegate.progressView.progress+=0.01;
       // self.delegate.progressView.progress+=0.01;
    
}
@end


<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">//</span><span style="white-space: pre;">ViewController.h</span>
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIProgressView *progressView;

@end

#import "ViewController.h"
#import "MyOperation.h"
#import "ProgressOperation.h"
#import "backAccount.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)threeOperationQueue:(UIButton *)sender
{
    MyOperation*op1=[[MyOperation alloc]init];
    op1.name=@"1";
    MyOperation *op2=[[MyOperation alloc]init];
    op2.name=@"2";
    MyOperation *op3=[[MyOperation alloc]init];
    op3.name=@"3";
    NSOperationQueue*queue=[[NSOperationQueue alloc]init];
    
//    queue.maxConcurrentOperationCount=1;
//   
////    改变优先级需要在放入队列前完成
//    [op3 setQueuePriority:NSOperationQueuePriorityVeryHigh];
//    [op1 setQueuePriority:NSOperationQueuePriorityLow];
//    
////    操作对象1依赖操作对象3,3完成后才执行1
//    [op1 addDependency:op3];
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
}
- (IBAction)updateProgressVew:(UIButton *)sender
{
    ProgressOperation *po=[[ProgressOperation alloc]init];
    po.delegate=self;
    
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    [queue addOperation:po];

    
}
- (IBAction)fetchMoney:(UIButton *)sender
{
    backAccount *hus=[[backAccount alloc]init];
    hus.name=@"hus";
    
    backAccount*wife=[[backAccount alloc]init];
    wife.name=@"wife";
    
    NSOperationQueue *queue=[[NSOperationQueue alloc ]init];
    //    指定操作队列最大并行操作数量完成同步,此处设为1,相当于同步。避免取钱,买票等异步引起的问题。这行不能放在addoperation之后。
    queue.maxConcurrentOperationCount=1;
    
    [queue addOperation:hus];
    [queue addOperation:wife];
    
    //NSOperation对象是一个single-shot(一次性)对象,(此处为hus,wife)当它执行完一遍后,便不能再次使用,下面代码出错!
    /*NSOperationQueue *operationQueue2 = [[NSOperationQueue alloc]init];
     [operationQueue2 addOperation:husband];
     [operationQueue2 addOperation:wife];*/

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end









//ViewController.h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值