CocoaPods trunk is moving to be read-only. Read more on the blog, there are 11 months to go.
| TestsTested | ✗ |
| LangLanguage | Obj-CObjective C |
| License | MIT |
| ReleasedLast Release | Aug 2016 |
Maintained by Mert Buran.
MBTaskContainer is a simple class that lets you add your NSURLSessionTasks into it and read active tasks from it safely.
Let's say your server use OAuth2 authentication standard, so you need to obtain a token before establishing a connection to access your API
And again, let's say your application has an architecture like the following:
ViewController -> DataController
where
1. ViewController
UIViewController subclassUIButton to cancel ongoing network operations
DataControllerNSObject subclassIn that case, we cannot return actual ongoing network tasks to ViewController from DataController immediately as even a simple network request would be like the following:
ViewController calls DataController's getItems methodDataController makes the request401 Unauthorized responsibleDataController makes another request to obtain a new tokenDataController retries getItems requestIn short, there will be many request that are done asynchronously and some follow the others.
In order to address this problem, you can use MBTaskContainer!
MBTaskContainer instance from DataController:getItems.ViewController can access active network tasks at any time.MBTaskContainer lets you add/read tasks from multiple threads in a safe way.
MBTaskContainer takes care of removal. You don't need to remove completed tasks.// DataController.m
- (MBTaskContainer *)seriallyGetRepositoriesOrganizationsMembers {
MBTaskContainer *taskContainer = [MBTaskContainer new];
NSURLSessionTask *repositories = [self fetchDataWithRelativeURL:repositoriesURL completion:^(NSError *error) {
if (error == nil) {
NSURLSessionTask *organizations = nil;
organizations = [self fetchDataWithRelativeURL:organizationsURL completion:^(NSError *error) {
if (error == nil) {
NSURLSessionTask *members = nil;
members = [self fetchDataWithRelativeURL:membersURL completion:nil];
[taskContainer addTask:members];
}
}];
[taskContainer addTask:organizations];
}
}];
[taskContainer addTask:repositories];
return taskContainer;
}
// ViewController.m
...
self.serialTaskContainer = [self.dataController seriallyGetRepositoriesOrganizationsMembers];
NSArray *activeTasks = [self.serialTaskContainer getTasks];
...
// To cancel ongoing and potential new tasks
self.serialTaskContainer.state = MBTaskContainerStateCancelling;
...MBTaskContainer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "MBTaskContainer"Mert Buran, [email protected]
MBTaskContainer is available under the MIT license. See the LICENSE file for more info.