Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#Created by http://www.gitignore.io

### Xcode ###
build
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcworkspace/contents.xcworkspacedata


### Objective-C ###
# OS X
.DS_Store

# Xcode
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
profile
*.moved-aside
DerivedData
*.hmap
*.ipa

# CocoaPods
Pods
Binary file modified NSThreadExample/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
45914ED418D81C5700C9B9C0 /* WTThreadDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 45914ED318D81C5700C9B9C0 /* WTThreadDownloader.m */; };
45914ED718D8230200C9B9C0 /* WTOperationDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 45914ED618D8230200C9B9C0 /* WTOperationDownloader.m */; };
ADB86C77177F613700E9AEC6 /* PTOperationDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = ADB86C76177F612B00E9AEC6 /* PTOperationDownloader.m */; };
F40E9FEB177A8E7900692CB2 /* PTThreadDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = F40E9FE8177A8E5800692CB2 /* PTThreadDownloader.m */; };
F40E9FEC177A8E7900692CB2 /* PTNormalDownloaler.m in Sources */ = {isa = PBXBuildFile; fileRef = F40E9FEA177A8E6E00692CB2 /* PTNormalDownloaler.m */; };
Expand Down Expand Up @@ -37,6 +39,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
45914ED218D81C5700C9B9C0 /* WTThreadDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTThreadDownloader.h; sourceTree = "<group>"; };
45914ED318D81C5700C9B9C0 /* WTThreadDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WTThreadDownloader.m; sourceTree = "<group>"; };
45914ED518D8230200C9B9C0 /* WTOperationDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTOperationDownloader.h; sourceTree = "<group>"; };
45914ED618D8230200C9B9C0 /* WTOperationDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WTOperationDownloader.m; sourceTree = "<group>"; };
ADB86C75177F612B00E9AEC6 /* PTOperationDownloader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PTOperationDownloader.h; sourceTree = "<group>"; };
ADB86C76177F612B00E9AEC6 /* PTOperationDownloader.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PTOperationDownloader.m; sourceTree = "<group>"; };
F40E9FE7177A8E5800692CB2 /* PTThreadDownloader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PTThreadDownloader.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -132,6 +138,10 @@
F40E9FE8177A8E5800692CB2 /* PTThreadDownloader.m */,
ADB86C75177F612B00E9AEC6 /* PTOperationDownloader.h */,
ADB86C76177F612B00E9AEC6 /* PTOperationDownloader.m */,
45914ED218D81C5700C9B9C0 /* WTThreadDownloader.h */,
45914ED318D81C5700C9B9C0 /* WTThreadDownloader.m */,
45914ED518D8230200C9B9C0 /* WTOperationDownloader.h */,
45914ED618D8230200C9B9C0 /* WTOperationDownloader.m */,
F44D2A5C17798A5C00B48C77 /* Images.xcassets */,
F44D2A4B17798A5C00B48C77 /* Supporting Files */,
);
Expand Down Expand Up @@ -268,7 +278,9 @@
ADB86C77177F613700E9AEC6 /* PTOperationDownloader.m in Sources */,
F40E9FEB177A8E7900692CB2 /* PTThreadDownloader.m in Sources */,
F40E9FEC177A8E7900692CB2 /* PTNormalDownloaler.m in Sources */,
45914ED418D81C5700C9B9C0 /* WTThreadDownloader.m in Sources */,
F44D2A5517798A5C00B48C77 /* PTAppDelegate.m in Sources */,
45914ED718D8230200C9B9C0 /* WTOperationDownloader.m in Sources */,
F44D2A5B17798A5C00B48C77 /* PTViewController.m in Sources */,
F44D2A5117798A5C00B48C77 /* main.m in Sources */,
);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
Expand Down
58 changes: 46 additions & 12 deletions NSURLConnectionExample/NSURLConnectionExample/PTViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import "PTNormalDownloaler.h"
#import "PTThreadDownloader.h"
#import "PTOperationDownloader.h"
#import "WTThreadDownloader.h"
#import "WTOperationDownloader.h"

@interface PTViewController ()

Expand All @@ -21,9 +23,13 @@ - (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(100, 100, 100, 30);
button.frame = CGRectMake(100, 30, 100, 30);
[button addTarget:self action:@selector(toggleButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 320, 400)];
imageView.tag = 123456;
[self.view addSubview:imageView];
}

- (void)didReceiveMemoryWarning
Expand All @@ -49,16 +55,16 @@ - (void)toggleButton
// }];

//Use PTThreadDownloaler
PTThreadDownloader *downloader = [PTThreadDownloader
downloadWithURL:[NSURL URLWithString:URLString]
timeoutInterval:15
success:^(id responseData){
NSLog(@"get data size: %d", [(NSData *)responseData length]);
NSLog(@"success block in main thread?: %d", [NSThread isMainThread]);
}
failure:^(NSError *error){
NSLog(@"failure block in main thread?: %d", [NSThread isMainThread]);
}];
// PTThreadDownloader *downloader = [PTThreadDownloader
// downloadWithURL:[NSURL URLWithString:URLString]
// timeoutInterval:15
// success:^(id responseData){
// NSLog(@"get data size: %d", [(NSData *)responseData length]);
// NSLog(@"success block in main thread?: %d", [NSThread isMainThread]);
// }
// failure:^(NSError *error){
// NSLog(@"failure block in main thread?: %d", [NSThread isMainThread]);
// }];


//Use PTOperationDownloader
Expand All @@ -76,7 +82,35 @@ - (void)toggleButton
// NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// [queue addOperation:downloader];NSBlockOperation

//Use WTThreadDownloader
// WTThreadDownloader *downloader = [WTThreadDownloader downloadWithURL:[NSURL URLWithString:URLString] timeoutInterval:15 success:^(id responseData)
// {
// dispatch_async(dispatch_get_main_queue(), ^
// {
// UIImageView *imageView = (UIImageView *)[self.view viewWithTag:123456];
// UIImage *image = [UIImage imageWithData:(NSData *)responseData];
// imageView.image = image;
// });
// }
// failure:^(NSError *error)
// {
// NSLog(@"Failed WTThreadDownloader: %@",error);
// }];

//Use WTOperationDownloader
WTOperationDownloader *downloader = [WTOperationDownloader downloadWithURL:[NSURL URLWithString:URLString] timeoutInterval:15 success:^(id responseData)
{
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:123456];
UIImage *image = [UIImage imageWithData:(NSData *)responseData];
imageView.image = image;
}
failure:^(NSError *error)
{
NSLog(@"Failed WTThreadDownloader: %@",error);
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:downloader];

NSLog(@"started downloader: %@", downloader.URL.absoluteString);
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// WTOperationDownloader.h
// NSURLConnectionExample
//
// Created by Joywii on 14-3-18.
// Copyright (c) 2014年 KiloApp. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void (^completionBlock)();

@interface WTOperationDownloader : NSOperation

@property (nonatomic, readwrite, retain) NSURL *URL;

+ (id)downloadWithURL:(NSURL *)URL
timeoutInterval:(NSTimeInterval)timeoutInterval
success:(void (^)(id responseData))success
failure:(void (^)(NSError *error))failure;

@end
Loading