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

前端之家收集整理的这篇文章主要介绍了10.2 NSOperation/NSOperationQueue:提供了一些在GCD中不容易实现的特性,如:限制最大并发数量,操作之间的依赖关系.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


<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);">//</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
原文链接:https://www.f2er.com/javaschema/284713.html

猜你在找的设计模式相关文章