No background sync after restarting browser - service-worker

I am implementing PWA with a service worker, utilizing Background Sync and IndexedDB in uploading images while user is offline.
So it is working! While offline on uploading an image, nativeFile is pushed to IndexedDB and a unique tag is registered for background sync. After bringing up network to online, the background sync called my function to upload the file from indexedDB to server. That happens when my browser is open.
HOWEVER, in the scenario where uploading while being offline. Then restart the browser and go online, no background sync is triggered after opening it.
Is this how supposed background sync will work? only when browser is open? or am I doing something wrong?

Related

Download large files in iOS

I wish to download large file (about: 500MB) from server.
The server is implemented by us server with gRPC command that works as follows. The user request for file and then the app receives byte stream which added to file.
In Android I am using background persistent notification with service, and opening new thread on this service until the download completes. The user can exit the app and the OS will not kill persistent notification service.
What is the equivalent for background downloading for iOS. I checked the URLSessionDownloadTask but I do not think it will work with gRPC.
What is your solution for downloading big data with iOS?

Force a POST to Background sync regardless of current connectivity - behaviour on Firefox and Safari?

So I've managed to implement Background Sync with Workbox, working beautifully on Chrome. Rather than wait for a fetch to fail, I'm immediately pushing it into the bg sync queue - so that users will never have to wait for an image upload, even if they have network connectivity.
Now, on Safari and Firefox - I understand the fallback will be to attempt a background sync every time the service worker starts. However, in this case the service worked has not stopped because there has not been an interruption in network service. Does this mean I will only get this item uploaded the next time the browser is started, or the specific PWA is started, or the URL is refreshed?
To answer your question, it's impossible to know for sure when a service worker will stop running. Each browser will stop a service worker after a period of time without any events, and then start up the service worker again the next time there's an event it needs to handle (fetch, sync, etc.) But each browser implements that exact timing differently.
Workbox's background sync implementation does have a manual method that could be called to start processing items in the queue on demand.
That being said, I would not recommend interacting with the background sync queue in that fashion.
Actually attempting the upload, and then only adding items to the background sync queue upon failure (automatically, via the BackgroundSyncPlugin would be easiest) is the recommended usage.
The behavior you really want, in which uploads "happen in the background", is enabled via background fetch. Unfortunately, background fetch is also only available in Chrome at this time. I'm not sure that trying to simulate background fetch in browsers that don't support it, and don't support background sync, is going to give non-Chrome users a great experience.

NSUrlSession suspend and resume issue on device lock

I am uploading file to server by using multipart form data with NSURLSession. When the application goes in background I want to suspend the request and resume when application comes in foreground again. So I simply do [session suspend] and [session resume]. This is working fine when the app goes in background only. But if the device gets locked, when going back in foreground and try to resume, I get a network connection lost error. I understand that when device is locked, all open sockets are closed and therefore the issue, but is there some way to make this work without the need of starting the upload from beginning?
You should switch your foreground session to a background session before the app goes to background, and then there is no need to suspend it. Your file will be uploaded by OS while in background (eventually).
Unfortunately, according to the documentation, you need to use a file to perform your background upload.
From "Background Transfer Considerations":
Only upload tasks from a file are supported (uploading from data objects or a stream will fail after the program exits).
(In addition to that, there is no guarantee when or why your app will be terminated. Trying to avoid device locking alone won't be sufficient; there are a myriad other ways your app may be terminated.)

Swift/iOS - Syncing Offiline Core Data with Server when Device is Back Online

I'm trying to understand how I can send my offline data to the server when the device is online.
Can this be achieved without having the user to open the app, and send the data immediately when the device is connected to the Internet in background?
One viable approach would utilise file uploads with a "background session".
What it requires is a file that represents your Core Data objects. When you have that, create a NSURLSession object with a background configuration using static function backgroundSessionConfigurationWithIdentifier(_:).
When you schedule a task with the background session, it will be executed by the system in the background, even when your app is terminated. The system only tries to download the file when it has a connection.
For more information see Using NSURLSession and Handling iOS Background Activity.
There are a few configuration options (NSURLSessionConfiguration) which may seem important in your case, too:
discretionary and sessionSendsLaunchEvents

Bring terminated iOS app to Foreground state once internet is available

App works without internet and I'm storing user data in CSV file using xcode. When internet is available, I want to send this CSV file over the net weather the app is in terminated or suspended state. Is there a way around to bring app in foreground/background state from terminated/suspended state when internet is available?
Yes, but it will be pretty annoying.
Whenever you loose a server connection to an app's instance, schedule a push notification. When that gets handled (e.g. the device is online and receiving the push), you may start a background task that uploads the data.
Besides abusing the push notification service and annoying your users (because of the decreasing battery life from the persistent networking and constant push notifications), you will need a massive back end to hold connections to all active app installations.
So, yes, but please don't.
Edit:
Since the data may only change when the app is 'alive', can't you rely on background fetch and background upload tasks, queueing unsuccessful uploads?

Resources