用NSOperation写下载队列

说明

1. 支持缓存机制

2. 图片都是在主线程中加载

3. 文件名用了md5加密

*这东西被人写烂了,但大伙如果对NSOperation不熟悉的话,可以看看本人的实现.

源码

https://github.com/YouXianMing/NSOperationExample

//
// ImageDownloadOperation.h
// NSOperationDownloadImage
//
// Created by YouXianMing on 15/9/7.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
@class ImageDownloadOperation; @protocol ImageDownloadOperationDelegate <NSObject> @required
- (void)imageDownloadOperation:(ImageDownloadOperation *)operation data:(NSData *)data; @end @interface ImageDownloadOperation : NSOperation { BOOL _executing;
BOOL _finished;
} /**
* 代理
*/
@property (nonatomic, weak) id <ImageDownloadOperationDelegate> delegate; /**
* 图片地址
*/
@property (nonatomic, strong) NSString *imageUrlString; /**
* 便利构造器
*
* @param urlString 图片地址
* @param delegate 代理
*
* @return 实例对象
*/
+ (instancetype)operationWithImageUrlString:(NSString *)urlString
delegate:(id <ImageDownloadOperationDelegate>)delegate; @end
//
// ImageDownloadOperation.m
// NSOperationDownloadImage
//
// Created by YouXianMing on 15/9/7.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ImageDownloadOperation.h"
#import <CommonCrypto/CommonDigest.h> @interface ImageDownloadOperation () @property (nonatomic, strong) NSURLConnection *connection;
@property (nonatomic, strong) NSString *md5String;
@property (nonatomic, strong) NSString *filePathString; @end @implementation ImageDownloadOperation - (void)main { // 验证图片地址是否为空
if (_imageUrlString.length <= ) { [self delegateEventWithData:nil];
[self completeOperation]; return;
} // 生成文件路径
self.md5String = [self MD5HashWithString:_imageUrlString];
self.filePathString = [self pathWithFileName:self.md5String]; // 文件如果存在则直接读取
BOOL exist = [[NSFileManager defaultManager] fileExistsAtPath:self.filePathString isDirectory:nil];
if (exist) { [self delegateEventWithData:[NSData dataWithContentsOfFile:self.filePathString]];
[self completeOperation]; return;
} NSURL *url = [NSURL URLWithString:_imageUrlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; // 让线程不结束
do { @autoreleasepool { [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; if (self.isCancelled) { [self completeOperation];
}
} } while (self.isExecuting && self.isFinished == NO);
} #pragma mark - 网络代理
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self writeData:data toPath:self.filePathString];
[self delegateEventWithData:data];
} - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self completeOperation];
} - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self delegateEventWithData:nil];
[self completeOperation];
} #pragma mark -
+ (instancetype)operationWithImageUrlString:(NSString *)urlString
delegate:(id <ImageDownloadOperationDelegate>)delegate { ImageDownloadOperation *operation = [[ImageDownloadOperation alloc] init];
operation.delegate = delegate;
operation.imageUrlString = urlString; return operation;
} #pragma mark -
- (void)completeOperation { [self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
_executing = NO;
_finished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
} - (void)start { if ([self isCancelled]) { [self willChangeValueForKey:@"isFinished"];
_finished = YES;
[self didChangeValueForKey:@"isFinished"]; return;
} [self willChangeValueForKey:@"isExecuting"];
[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
_executing = YES;
[self didChangeValueForKey:@"isExecuting"];
} - (BOOL)isExecuting { return _executing;
} - (BOOL)isFinished { return _finished;
} - (BOOL)isConcurrent { return YES;
} #pragma mark -
- (NSString *)MD5HashWithString:(NSString *)string { CC_MD5_CTX md5; CC_MD5_Init(&md5);
CC_MD5_Update(&md5, [string UTF8String], (CC_LONG) [string length]); unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5); NSString *s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[],
digest[], digest[]]; return s;
} - (NSString *)pathWithFileName:(NSString *)name { NSString *path = [NSString stringWithFormat:@"/Documents/%@", name];
return [NSHomeDirectory() stringByAppendingPathComponent:path];
} - (void)delegateEventWithData:(NSData *)data { if (_delegate && [_delegate respondsToSelector:@selector(imageDownloadOperation:data:)]) { dispatch_async(dispatch_get_main_queue(), ^{ [_delegate imageDownloadOperation:self data:data];
});
}
} - (void)writeData:(NSData *)data toPath:(NSString *)path { dispatch_async(dispatch_get_global_queue(, ), ^{ [data writeToFile:path atomically:YES];
});
} @end

细节

用NSOperation写下载队列的更多相关文章

  1. Objective-c 多线程操作 自定义NSOperation 模拟下载

    写在前面 使用多线程下载图片,使用内存缓存和磁盘缓存. 这里只为理解NSOperation及其派生类 真要应用到APP中 请下载成熟的第三方库 效果 下载多张图片时可控制线程并发数 分析 自定义NSO ...

  2. [翻译] 使用开源库 JGDownloadAcceleration 控制下载队列,断点下载,加速下载

    JGDownloadAcceleration 本人对原文进行了翻译,凑合看看,使用心得以后补上 https://github.com/JonasGessner/JGDownloadAccelerati ...

  3. Java语言实现简单FTP软件------>上传下载队列窗口的实现(七)

    1.首先看一下队列窗口的界面 2.看一下上传队列窗口的界面 3.看一下下载队列窗口的界面 4.队列窗口的实现 package com.oyp.ftp.panel.queue; import stati ...

  4. WorldWind源码剖析系列:下载队列类DownloadQueue

    下载队列类DownloadQueue代表具有优先级的下载队列,该类的存储下载请求的数组链表专门按一定的优先级来存储下载请求的.该类的类图如下. 下载队列类DownloadQueue各个字段的含义说明如 ...

  5. 手写阻塞队列(Condition实现)

    自己实现阻塞队列的话可以采用Object下的wait和notify方法,也可以使用Lock锁提供的Condition来实现,本文就是自己手撸的一个简单的阻塞队列,部分借鉴了JDK的源码.Ps:最近看面 ...

  6. Netty源码分析第7章(编码器和写数据)---->第3节: 写buffer队列

    Netty源码分析七章: 编码器和写数据 第三节: 写buffer队列 之前的小节我们介绍过, writeAndFlush方法其实最终会调用write和flush方法 write方法最终会传递到hea ...

  7. PHP怎样写延时队列(定时器)

    背景 PHP没有定时器,依托的都是crontab这样的系统工具,也没有go中defer这样的延时方法,本文介绍几种PHP写延时队列的几种姿势. 延时队列的定义 普通的队列是先进先出,但是延时队列并不是 ...

  8. 弹出窗口a标签写下载,再弹出窗口

    如果这个窗口是弹出出口,直接<a href="">点击下载<a>是不行的,得用js这样写,弹出并关闭,不然会回到首页,如果没有定义首页会报错,<a h ...

  9. 三 基于Java数组手写循环队列

    Code: package dataStucture2.stackandqueue; /** * 手写循环队列 * * @param <E> */ public class MyLoopQ ...

随机推荐

  1. WPF Style和Template

    WPF中的Style类似于Web应用程序中的CSS,它是控件的一个属性,属于资源的一种. ControlTemplate和DataTemplate区别: ControlTemplate用于改变控件原来 ...

  2. redis-java中的callback回掉机制

    springboot整合redis后, 会提供StringRedisTEmplate和 RedisTemplate 两个模板类供食用, 有时候这并不能满足我们的需求, 需要使用 connect 处理, ...

  3. PM2怎么开启ES6?

    node版本v6.14.2,无论是用配置文件 { "apps": [{ "name": "server", "script&quo ...

  4. 纯Java JDBC连接数据库,且用JDBC实现增删改查的功能

    Java JDBC连接数据库 package cn.cqvie.yjq; import java.sql.*; /** * 注册数据库的驱动程序,并得到数据库的连接对象 * @author yu * ...

  5. 【BATJ面试必会】Java 基础篇

    一.数据类型 包装类型 缓存池 二.String 概览 不可变的好处 String, StringBuffer and StringBuilder String Pool new String(&qu ...

  6. lazy-mock ,一个生成后端模拟数据的懒人工具

    lazy-mock   lazy-mock 是基于koa2构建的,使用lowdb持久化数据到JSON文件.只需要简单的配置就可以实现和json-server差不多的功能,但是比json-server更 ...

  7. IE浏览器版本的判断

    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 && userAgent.indexOf(; //判断是否IE< ...

  8. 淺談 Entity 的概念

    延續上一篇文章<Drupal Commerce 概念架構>,本來打算要繼續講 Commerce 與 Views 整合的主題.不過由於這個主題牽涉到 Views 中的 Relationshi ...

  9. 用PDMReader工具生成数据库设计文档(转载)

    来源:http://blog.csdn.net/xinglun88/article/details/19987719 第一步:下载并安装PDMReader,资源网站: http://www.pdmre ...

  10. 什么是SOA

    面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.接口是采用中立的方式进行定义的,它应该独立于实现服务的硬件平台.操作系统和编 ...