Skip to content

Commit

Permalink
Instance methods, nullability + simplification
Browse files Browse the repository at this point in the history
- The category is now using instance methods as suggested by @AliThink
- Added nullability specifiers for better compatibility with Swift
- Removed half of the code by taking advantage of the „advanced“
counterpart method (e.g. by creating an NSURLRequest from a given NSURL)
  • Loading branch information
floschliep committed Oct 15, 2015
1 parent 1f7224b commit db2cc71
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 71 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014 Florian Schliep (http://floschliep.com/)
Copyright (c) 2015 Florian Schliep (http://floschliep.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 7 additions & 7 deletions NSURLSession+SynchronousTask/NSURLSession+SynchronousTask.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// NSURLSession+SynchronousTask.h
//
// Copyright (c) 2014 Florian Schliep (http://floschliep.com/)
// Copyright (c) 2015 Florian Schliep (http://floschliep.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,17 +27,17 @@

#pragma mark - NSURLSessionDataTask

+ (NSData *)sendSynchronousDataTaskWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
+ (NSData *)sendSynchronousDataTaskWithURL:(NSURL *)url returningResponse:(NSURLResponse **)response error:(NSError **)error;
- (nullable NSData *)sendSynchronousDataTaskWithURL:(nonnull NSURL *)url returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error;
- (nullable NSData *)sendSynchronousDataTaskWithRequest:(nonnull NSURLRequest *)request returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error;

#pragma mark - NSURLSessionDownloadTask

+ (NSURL *)sendSynchronousDownloadTaskWithURL:(NSURL *)url returningResponse:(NSURLResponse **)response error:(NSError **)error;
+ (NSURL *)sendSynchronousDownloadTaskWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
- (nullable NSURL *)sendSynchronousDownloadTaskWithURL:(nonnull NSURL *)url returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error;
- (nullable NSURL *)sendSynchronousDownloadTaskWithRequest:(nonnull NSURLRequest *)request returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error;

#pragma mark - NSURLSessionUploadTask

+ (NSData *)sendSynchronousUploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData returningResponse:(NSURLResponse **)response error:(NSError **)error;
+ (NSData *)sendSynchronousUploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL returningResponse:(NSURLResponse **)response error:(NSError **)error;
- (nullable NSData *)sendSynchronousUploadTaskWithRequest:(nonnull NSURLRequest *)request fromFile:(nonnull NSURL *)fileURL returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error;
- (nullable NSData *)sendSynchronousUploadTaskWithRequest:(nonnull NSURLRequest *)request fromData:(nonnull NSData *)bodyData returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error;

@end
68 changes: 13 additions & 55 deletions NSURLSession+SynchronousTask/NSURLSession+SynchronousTask.m
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// NSURLSession+SynchronousTask.m
//
// Copyright (c) 2014 Florian Schliep (http://floschliep.com/)
// Copyright (c) 2015 Florian Schliep (http://floschliep.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,28 +27,14 @@ @implementation NSURLSession (SynchronousTask)

#pragma mark - NSURLSessionDataTask

+ (NSData *)sendSynchronousDataTaskWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSData *data = nil;
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
data = taskData;
if (response) {
*response = taskResponse;
}
if (error) {
*error = taskError;
}
dispatch_semaphore_signal(semaphore);
}] resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

return data;
- (nullable NSData *)sendSynchronousDataTaskWithURL:(nonnull NSURL *)url returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error {
return [self sendSynchronousDataTaskWithRequest:[NSURLRequest requestWithURL:url] returningResponse:response error:error];
}

+ (NSData *)sendSynchronousDataTaskWithURL:(NSURL *)url returningResponse:(NSURLResponse **)response error:(NSError **)error {
- (nullable NSData *)sendSynchronousDataTaskWithRequest:(nonnull NSURLRequest *)request returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSData *data = nil;
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
[[self dataTaskWithRequest:request completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
data = taskData;
if (response) {
*response = taskResponse;
Expand All @@ -65,28 +51,14 @@ + (NSData *)sendSynchronousDataTaskWithURL:(NSURL *)url returningResponse:(NSURL

#pragma mark - NSURLSessionDownloadTask

+ (NSURL *)sendSynchronousDownloadTaskWithURL:(NSURL *)url returningResponse:(NSURLResponse **)response error:(NSError **)error {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSURL *location = nil;
[[[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL *taskLocation, NSURLResponse *taskResponse, NSError *taskError) {
location = taskLocation;
if (response) {
*response = taskResponse;
}
if (error) {
*error = taskError;
}
dispatch_semaphore_signal(semaphore);
}] resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

return location;
- (nullable NSURL *)sendSynchronousDownloadTaskWithURL:(nonnull NSURL *)url returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error {
return [self sendSynchronousDownloadTaskWithRequest:[NSURLRequest requestWithURL:url] returningResponse:response error:error];
}

+ (NSURL *)sendSynchronousDownloadTaskWithRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error {
- (nullable NSURL *)sendSynchronousDownloadTaskWithRequest:(nonnull NSURLRequest *)request returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSURL *location = nil;
[[[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *taskLocation, NSURLResponse *taskResponse, NSError *taskError) {
[[self downloadTaskWithRequest:request completionHandler:^(NSURL *taskLocation, NSURLResponse *taskResponse, NSError *taskError) {
location = taskLocation;
if (response) {
*response = taskResponse;
Expand All @@ -103,28 +75,14 @@ + (NSURL *)sendSynchronousDownloadTaskWithRequest:(NSURLRequest *)request return

#pragma mark - NSURLSessionUploadTask

+ (NSData *)sendSynchronousUploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData returningResponse:(NSURLResponse **)response error:(NSError **)error {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSData *data = nil;
[[[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
data = taskData;
if (response) {
*response = taskResponse;
}
if (error) {
*error = taskError;
}
dispatch_semaphore_signal(semaphore);
}] resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

return data;
- (nullable NSData *)sendSynchronousUploadTaskWithRequest:(nonnull NSURLRequest *)request fromFile:(nonnull NSURL *)fileURL returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error {
return [self sendSynchronousUploadTaskWithRequest:request fromData:[NSData dataWithContentsOfURL:fileURL] returningResponse:response error:error];
}

+ (NSData *)sendSynchronousUploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL returningResponse:(NSURLResponse **)response error:(NSError **)error {
- (nullable NSData *)sendSynchronousUploadTaskWithRequest:(nonnull NSURLRequest *)request fromData:(nonnull NSData *)bodyData returningResponse:(NSURLResponse *_Nullable*_Nullable)response error:(NSError *_Nullable*_Nullable)error {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSData *data = nil;
[[[NSURLSession sharedSession] uploadTaskWithRequest:request fromFile:fileURL completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
[[self uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData *taskData, NSURLResponse *taskResponse, NSError *taskError) {
data = taskData;
if (response) {
*response = taskResponse;
Expand Down
6 changes: 3 additions & 3 deletions NSURLSession-SynchronousTask.podspec
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|

s.name = "NSURLSession-SynchronousTask"
s.version = "1.0.0"
s.version = "1.1"
s.summary = "NSURLSession category which brings NSURLConnection-like synchronous tasks to NSURLSession"

s.description = <<-DESC
NSURLSession+SynchronousTask is an NSURLSession category which brings NSURLConnection-like (remember sendSynchronousRequest:returningResponse:error:?) synchronous tasks to NSURLSession.
Internally the category uses GCD to wait for the tasks to finish and NSURLSession's shared instance sharedSession to perform the tasks.
Internally the category uses GCD to wait for the tasks to finish.
DESC

s.homepage = "https://github.com/floschliep/NSURLSession-SynchronousTask"
Expand All @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = "7.0"
s.osx.deployment_target = "10.9"

s.source = { :git => "https://github.com/floschliep/NSURLSession-SynchronousTask.git", :tag => "1.0.0" }
s.source = { :git => "https://github.com/floschliep/NSURLSession-SynchronousTask.git", :tag => "1.1" }
s.source_files = "NSURLSession+SynchronousTask"

end
10 changes: 5 additions & 5 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
NSURLSession+SynchronousTask
============================

`NSURLSession+SynchronousTask` is an NSURLSession category which brings NSURLConnection-like (remember `sendSynchronousRequest:returningResponse:error:`?) synchronous tasks to NSURLSession.
`NSURLSession+SynchronousTask` is an `NSURLSession` category which brings `NSURLConnection`-like (remember `sendSynchronousRequest:returningResponse:error:`?) synchronous tasks to `NSURLSession`.

Internally the category uses GCD to wait for the tasks to finish and `NSURLSession`'s shared instance `sharedSession` to perform the tasks.
Internally the category uses GCD to wait for the tasks to finish.

## Installation

Expand All @@ -23,7 +23,7 @@ pod 'NSURLSession-SynchronousTask'
NSURL *url = ...
NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLSession sendSynchronousDataTaskWithURL:url returningResponse:&response error:&error];
NSData *data = [[NSURLSession sharedSession] sendSynchronousDataTaskWithURL:url returningResponse:&response error:&error];
...
```
### NSURLSessionDownloadTask
Expand All @@ -32,7 +32,7 @@ NSData *data = [NSURLSession sendSynchronousDataTaskWithURL:url returningRespons
NSURL *url = ...
NSError *error = nil;
NSURLResponse *response = nil;
NSURL *fileURL = [NSURLSession sendSynchronousDownloadTaskWithURL:url returningResponse:&response error:&error];
NSURL *fileURL = [[NSURLSession sharedSession] sendSynchronousDownloadTaskWithURL:url returningResponse:&response error:&error];
...
```
### NSURLSessionUploadTask
Expand All @@ -42,7 +42,7 @@ NSURLRequest *uploadRequest = ...
NSData *dataToBeUploaded = ...
NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLSession sendSynchronousUploadTaskWithRequest:uploadRequest fromData:dataToBeUploaded returningResponse:&response error:&error];
NSData *data = [[NSURLSession sharedSession] sendSynchronousUploadTaskWithRequest:uploadRequest fromData:dataToBeUploaded returningResponse:&response error:&error];
...
```

Expand Down

0 comments on commit db2cc71

Please sign in to comment.