Skip to content

Commit

Permalink
Merge pull request #8 from josipbernat/develop
Browse files Browse the repository at this point in the history
Added background task support
  • Loading branch information
josipbernat committed Sep 30, 2014
2 parents 917f7ae + ae3d607 commit b261b42
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
4 changes: 2 additions & 2 deletions JBMessage.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = "JBMessage"
s.version = "1.0.14"
s.version = "1.0.15"
s.summary = "JBMessage is simple iOS networking wrapper based on AFNetworking"
s.homepage = "https://github.com/josipbernat/JBMessage"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Josip Bernat" => "[email protected]" }
s.social_media_url = "http://twitter.com/josipbernat"
s.platform = :ios, "6.0"
s.source = { :git => "https://github.com/josipbernat/JBMessage.git", :tag => "v1.0.14" }
s.source = { :git => "https://github.com/josipbernat/JBMessage.git", :tag => "v1.0.15" }
s.source_files = 'JBMessage/JBMessage/**/*.{h,m}'
s.requires_arc = true
s.dependency "AFNetworking", "~> 2.4"
Expand Down
1 change: 1 addition & 0 deletions JBMessage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@
GCC_PREFIX_HEADER = "JBMessage/JBMessage-Prefix.pch";
INFOPLIST_FILE = "JBMessage/JBMessage-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
WRAPPER_EXTENSION = app;
};
name = Release;
Expand Down
5 changes: 5 additions & 0 deletions JBMessage/JBMessage/JBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ typedef void (^JBDownloadBlock)(NSUInteger bytesRead, long long totalBytesRead,
*/
@property (nonatomic, readwrite) BOOL shouldParseResponseOnMainQueue;

/**
* Boolean value determening whether operation should continue network task in background. Default is NO. Setting this property when message is in execution will have no effect.
*/
@property (nonatomic, readwrite) BOOL shouldContinueAsBackgroundTask;

/**
* A file URL for the multipart request. Deprecated from V.1.0.9. See inputFileURL and outputFileStreamPath.
*/
Expand Down
51 changes: 28 additions & 23 deletions JBMessage/JBMessage/JBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ @interface JBMessage () {
BOOL _isCancelled;
BOOL _isFinished;
BOOL _isExecuting;

id _willResignObserver;
}

@property (nonatomic, strong) AFHTTPRequestOperation *operation;
Expand Down Expand Up @@ -63,8 +65,13 @@ @implementation JBMessage
#pragma mark - Memory Management

- (void) dealloc {

_responseBlock = nil;
_uploadBlock = nil;

if (_willResignObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:_willResignObserver];
}
}

#pragma mark - Shared Queue
Expand Down Expand Up @@ -194,39 +201,31 @@ - (void)initialize {
_responseSerializer = JBResponseSerializerTypeHTTP;
_shouldParseResponseOnMainQueue = YES;
_timeoutInterval = 60.0f;
}

#pragma mark - Background Task

- (void)beginBackgroundTask {

self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
}

- (void)endBackgroundTask {

[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
__weak id this = self;
_willResignObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillResignActiveNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {

__strong typeof(self) strongThis = this;
if (strongThis.operation.isExecuting) {
[strongThis.operation cancel];
}
}];
}

#pragma mark - Operations

-(void) start {
- (void)start {

_isExecuting = YES;
_isFinished = NO;

[self operationDidStart];
}

- (void) finish {
[self endBackgroundTask];
}

- (void) cancel {
- (void)cancel {

_isCancelled = YES;
[self.operation cancel];
Expand Down Expand Up @@ -259,8 +258,6 @@ - (void)operationDidFinish {
[self willChangeValueForKey:@"isFinished"];
_isFinished = YES;
[self didChangeValueForKey:@"isFinished"];

[self finish];
}

#pragma mark - Executing Request
Expand Down Expand Up @@ -299,6 +296,14 @@ - (void)executeRequest {
[operation setCompletionQueue:jb_message_completion_callback_queue()];
}

if (self.shouldContinueAsBackgroundTask) {

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
__strong typeof(self) strongThis = this;
[strongThis.operation resume];
}];
}

[manager.operationQueue addOperation:operation];
}

Expand Down

0 comments on commit b261b42

Please sign in to comment.