0

I'm using an Alamofire request method live() for the users but they need 1 hour download at least.

I'd like to run the method in background.

But not a queue which resumes after turn back to the app, instead working in background.

Is there any way using any library or framework to run the method in background?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Possible duplicate of [To run app in background for long time in iphone](http://stackoverflow.com/questions/10702661/to-run-app-in-background-for-long-time-in-iphone) – Rob Dec 05 '16 at 17:02
  • Your question title is misleading. You don't want to run a function in the background - You want to set up an AlamoFire download that runs in the background. See my answer. – Duncan C Dec 05 '16 at 17:24
  • [This question](http://stackoverflow.com/questions/41021733/is-using-core-location-for-performing-functionality-in-the-background-appropriat) might be useful to your case. – Ahmad F Dec 07 '16 at 16:41

2 Answers2

1

The system class URLSession has the ability to download files from the background. That's likely the cleanest way to download your files. When you create a URLSession for background downloading then the system will notify you when the download is complete, even if it has to relaunch your app to do so.

You should be able to search on "URLSession background download Task" to learn more. There are a number of steps, and there are quite a few tutorials online explaining how to do it.

I don't know if AlamoFire exposes this ability or not.

EDIT:

It seems that AlamoFire does indeed support background downloading. See this link:

AlamoFire Download in Background Session

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
-2

There is one way for an app to run forever in background, and that's by calling requestLocationUpdates() from CoreLocation, but for that you need user's permission for Always Location and you'll also drain the battery quite a little. Other than that there isn't any method I'm aware of.

S2dent
  • 939
  • 6
  • 9
  • Okay, Can I execute any func() inside requestLocationUpdates() ? 'considering no problem about permission granting! ' – Ahmed El khallaf Dec 05 '16 at 16:56
  • You just call `requestLocationUpdates()` anywhere in your code and your app starts running in background forever. And you can call your download function on some thread and forget about it. – S2dent Dec 05 '16 at 16:57
  • un resolved identifier , I think the syntax have changed in Swift 3 ?! – Ahmed El khallaf Dec 05 '16 at 17:14
  • 3
    This is terrible advice because of the app isn't using core location for what it is meant for, Apple will reject the app. – rmaddy Dec 05 '16 at 17:15
  • 2
    I was about to post a "this is terrible advice" comment, but rmaddy beat me to it. Apple will indeed reject an app configured this way, and if they didn't, this approach would rapidly drain the user's battery. – Duncan C Dec 05 '16 at 17:23
  • The man is not wrong , I am using the app unofficially for my own home devices. Thanx for him , thanks for your advice too :) – Ahmed El khallaf Dec 05 '16 at 17:44
  • Yeah, I agree, this isn't the proper use of the API, but this just works, and yeah, that won't do it for AppStore but that's a way to `run a func() in background` and I have been using it for my own projects because even `URLSession` doesn't really do the job. Btw, it's https://developer.apple.com/reference/corelocation/cllocationmanager/1423750-startupdatinglocation – S2dent Dec 05 '16 at 18:03
  • What about [URLSessionConfiguration](https://developer.apple.com/reference/foundation/urlsessionconfiguration)? – Ahmad F Dec 08 '16 at 07:10