iOS Question The upload of files in the background

red30

Well-Known Member
Licensed User
Longtime User
I've asked a similar question before but never found an answer. I need to upload a lot of files to the server and ios really lacks background uploads for this ... I have to wait and not minimize or lock the phone until all the files are uploaded. This is very inconvenient ... In android, this issue is easily solved using services, but for b4i and ios I did not find a similar solution.
From the beginning, I thought that in ios it was generally impossible to upload something in the background, but I tried the viber and whatsapp applications and they can upload, for example, photos if you minimize applications and even if you lock your phone.
Maybe now there is some solution or a way to do this? Any example. Maybe someone already faced such a problem and found a solution?
 
Solution
My app uploads files in background. It's relatively simple. You need to change NSURLSession and then you can use standard httpjob.

I attached a sample, which I used before adding a code to my real app. This sample downloads files, but upload will work also.

Click any button, switch to another app, wait some time and activate a sample again. You will see that download is finished.

Of course, a background has some limitations. You need to start all downloads / uploads in foreground. JobDone fires in background, but in this moment I was not able to activate a new upload.

hatzisn

Well-Known Member
Licensed User
Longtime User
I would create a table in the db and place in it all the filenames (one in each record) and an indicator Uploaded (field) next to each. Then with silent push notifications I would create code that for each next silent push notification it will try to upload the next not uploaded file in the table and notify the server it has done so. Then the server would send a new silent push notification. The down side of this is that there would be a few seconds window to upload it (I think 30)...

Edit: A variation of this approach would be to upload specific amounts of bytes (or even less bytes than these amounts) in each round (if the file is long) and reassemble the file in the server.
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I would create a table in the db and place in it all the filenames (one in each record) and an indicator Uploaded (field) next to each. Then with silent push notifications I would create code that for each next silent push notification it will try to upload the next not uploaded file in the table and notify the server it has done so. Then the server would send a new silent push notification. The down side of this is that there would be a few seconds window to upload it (I think 30)...

Edit: A variation of this approach would be to upload specific amounts of bytes (or even less bytes than these amounts) in each round (if the file is long) and reassemble the file in the server.
That is, if I use silent push notifications, will it make the application work in the background? There are several problems here:
- I don't have access to the server and can't make it send me any notifications. I send pictures but I don’t know how I can break them into parts. Also, I can't make the server collect the file even if it's transferred partially.
- I have about 30 seconds to upload, which in conditions, for example, of poor mobile Internet, is not enough. It turns out that I cannot control the file uploading time and guarantee that it will take less than 30 seconds ...

But in general, if I understand the idea correctly, then if I make a silent push notification every 20 seconds, will this make the application continue working? Yes, I understand that this is inconvenient, but so far I see no other way ...
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Try to search in the forum the tutorial for Silent Push Notifications. You can also split the job - that is post partly the bytes in server 1 and host in a Raspberry Pi your own server which will be built in B4J and will receive the "I 'm done uploading partially the bytes, send the next push notification in a few seconds". In the tutorial of push notification (not in silent) there is code by @Erel which sends push notification. Also can't you create a php or .net backend which will receive a base64 encoded byte array and will recreate it and store it in a DB? Then when you are done you can reassemble it by getting the blobs from db. At last if the server belongs to someone else then contact them and offer them this solution.
 
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Another solution is post in your Raspberry Pi server partially and upload to the other server fully from it.
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Try to search in the forum the tutorial for Silent Push Notifications. You can also split the job - that is post partly the bytes in the server you don't have access and host in a Raspberry Pi your own server which will be built in B4J and will receive the "I 'm done uploading partially the bytes, send the next push notification in a few seconds". In the tutorial of push notification (not in silent) there is code by @Erel which sends push notification. Also can't you create a php or .net backend which will receive a base64 encoded byte array and will recreate it and store it in a DB? Then when you are done you can reassemble it by getting the blobs from db. At last if the server belongs to someone else then contact them and offer them this solution.
Another solution is post in your Raspberry Pi server partially and upload to the other server fully from it.
I understood you. Thank you for this solution, but it complicates the project very much. It's just that what I want to do seemed to me a simple and basic task that I thought b4i should support. After all, it is possible to send files in the background in ios...
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
My app uploads files in background. It's relatively simple. You need to change NSURLSession and then you can use standard httpjob.

I attached a sample, which I used before adding a code to my real app. This sample downloads files, but upload will work also.

Click any button, switch to another app, wait some time and activate a sample again. You will see that download is finished.

Of course, a background has some limitations. You need to start all downloads / uploads in foreground. JobDone fires in background, but in this moment I was not able to activate a new upload.
 

Attachments

  • s02.zip
    3.7 KB · Views: 145
Upvote 0
Solution

red30

Well-Known Member
Licensed User
Longtime User
My app uploads files in background. It's relatively simple. You need to change NSURLSession and then you can use standard httpjob.

I attached a sample, which I used before adding a code to my real app. This sample downloads files, but upload will work also.

Click any button, switch to another app, wait some time and activate a sample again. You will see that download is finished.

Of course, a background has some limitations. You need to start all downloads / uploads in foreground. JobDone fires in background, but in this moment I was not able to activate a new upload.
I tried your app, everything works just fine! Many thanks!
I even tried to block the phone and still the download was completed. It's just superb!
You write that JobDone will work in the background, but I won't be able to start a new upload. What is it connected with? Is this not possible on ios? How can applications (Viber, WhatsApp, Telegram) upload many photos in the background?
You also write that you can run several uploads at the same time in the foreground. I have never done this, I always waited for one file to be uploaded and only after that I started uploading the next one. If I need to upload 100 photos, how can I start uploading 100 photos at once in the foreground?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
In my app users fill, lets say, a questionnaire, which includes a text (10KB maximum) and a lot of photoes (can be 100, 200 MB).
What I do ... When user clicks "Send" an app sends a text part to the server in ordinary foreground regime.
A server reserves a place and confirms that it is ready to accept photoes. An app begins all (!) uploads (PostMultipart) and does not wait JobDone.
So, my app uploads a lot of photoes in the same moment (like in WhatsApp etc)

What is impossible... I wanted to send like in B4A (where I used a service). First photo, second photo etc.
But I was not able to add upload in JobDone, when an app is background. I did not find an answer in documentation. Guess, this is due to IOS design.
That's why my app starts all uploads and does not wait when previous upload will be finished.

What is not very comfortable - we needed to reconstruct the server software - the server can receive at first photo 2, then 5, no order.
 
Upvote 0
Top