Reputation: 223
My app needs to perform multiple URL requests via Alamofire but I would like to perform these tasks independently on views or what user does in UI. This basically means "on background" so the handlers which actually performing the tasks would not be deinitialized until they are done. My idea would be to call these requests from shared AppDelegate via some kind of class methods. I have only one question now:
What would be the best way to implement this scenario?
My actual knowledge would create something like this:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class func someKindOfClassRequest() {
// ...
}
func someKindOfRequest() {
// ...
}
// ...
}
And I would call the method this way:
AppDelegate.someKindOfClassRequest()
or with not-class func, which of course will not solve the issue:
let sharedDelegate = UIApplication.shared.delegate as! AppDelegate
AppDelegate.someKindOfRequest()
Upvotes: 2
Views: 539
Reputation: 2906
As mentioned in the comments, Alamofire uses closure-based completions so they will not get deinitialized even if the calling object is deinitialized. For the sake of keeping your code well organized, why not just create a class that does these things instead of throwing it into your AppDelegate
? For instance, create a class called BankgroundRequestController
:
class BackgroundRequestController {
static let sharedInstance = BackgroundRequestController()
class func someKindOfClassRequest() {
// ...
}
}
Then you can call these functions like:
BackgroundRequestController.sharedInstance.someKindOfClassRequest()
Upvotes: 4