How can I synchronize items with the cloud while my app is in the background? - objective-c

In my iPad app, the user can enter data online or offline, storing the data in SQLite, and when the user goes online, he hits a "sync" button, whereupon the data will be synced up to the cloud.
If the user enters data offline, the data persists in the local SQLite DB. If the data is not yet synced, I set a badge to the number of records pending sync.
When the user starts up his iPad, he should get some alert on startup saying "There are 5 records pending to sync." Then all the pending records will be synced, without opening my app.
Has anyone come across a similar scenerio? Does anyone have any idea how I can do that?

Before iOS 5.0 it was not possible to synchronize data with your application when the application was not running. However there were some scenarios when the app could e.g finish a download when the App was terminated but this background-processing is rather limited.
Now if you want to sync to the cloud and have the same data available on another iOS device or on a Mac app then I guess iCloud might be exactly what you are looking for.
So a possible scenario with iCloud might look like this:
You have your iCloud enabled App installed on two iPads
On both iPads you are logged in with your iCloud account (this has to match since iCloud is tied to a given Apple ID
When your App stores some data on iPad1 it will automatically be synced to the cloud
On the same time this data is downloaded to the iPad2 (which has to have internet connection) while your application is not yet launched. This is only possible with iCloud
When you now start your App on iPad2 (even if it is now offline) you will have the current data available within your App.
However, if you are running your own server backend, and maybe need more control over the data on the server, iCloud might not (yet) fit your needs there. You might want to check out the documentation or the sample code for iCloud: iCloud for Developers (Apple Developer Account required)
If you chose not to use iCloud then there is currently no way that you can sync data while your application is not running.
Edit:
A scenario without iCloud could look like this:
You enter Data on iPad1 while offline and set the badge to the number of unsynced items
Later you have internet connection, so you start the app and the items can be synced to your server.
Your server stores the newly arrived entries and could now send a Push notification to your App which is installed on iPad2 telling the user that there are n new entries on the server.
From that notification the user could now decide to open the app and the new items could then be downloaded to the iPad2.
If the user dismisses the notification then no data is downloaded until he starts your App the next time.

Related

Offline first React Native Expo app with background sync

I am looking at building an offline first React Native Expo app that automatically pushes data to an API when the device gets a connection. However I am struggling to see how if this is possible within Expo and need some guidance.
The app will need to store data from an API for offline use (presumably on first load, which will then be used to populate fields in a form). The form needs to work offline, with the input data stored on the device until it receives a new connection. At that point the app should push the data to an API (whilst the app is in the background). So I need to do multiple things:
Automatically download and store data from an API on first launch.
Store input data - from form fields whilst offline.
Background sync - Upload this user form data when a new connection is received, regardless of whether the app is running in the background or not.
Work on Android and iOS devices.
I have been looking at redux-offline, but unsure if it still supported and/or will satisfy all four requirements?
There is also redux-persist but I can't see how this satisfies the background sync?
Thanks for any help/guidance!
From my point of view, redux-persist is less opinionated compared to redux-offline. I pick redux-persist with useNetInfo hook to listen to network availability.
1. Automatically download and store data from an API on first launch.
When app launch, query initial data and dispatch to redux store and synced automatically to local storage by redux-persist
2. Store input data - from form fields whilst offline.
With useNetInfo, the app can detect network connectivity status, when not connected, dispatch to the redux store with a flag to indicate offline unsynched data.
You need to write a network connectivity listener which is executed when the network state changes and continuously track unsynched data and sync with the database when available and purge those temporary data like form values.
3. Background app sync
Expo SDK provides API https://docs.expo.dev/versions/latest/sdk/background-fetch/
to run continuously a background task at a certain time threshold.
You can sync offline data when the network returns back while the app is background.
Disclaimer: Background tasks run outside of React componentS tree. You can't access redux store data through the react-redux's store component or hooks based API.
redux-persist saves data with Async Storage and you need to directly access data as below.
AsyncStorage.getItem("persist:[REDUCER_CONTAIN_INTERESTED_DATA]")
This seems to be an issue with server state with a client cache. Looks like an ideal case for react-query to solve.
Automatically download and store data from an API on first launch.
This should be taken care of by query prefetching
Store input data - from form fields whilst offline.
Probably needs client state(redux/Zustand etc) for managing state with unverified fields. For storing submitted forms, react-query supports offline caching of 'mutations'.
Background sync - Upload this user form data when a new connection is received, regardless of whether the app is running in the background or not.
Work on Android and iOS devices.
See above.

Accessing the localStorage of one device from another in react-native

Some days before, i saw a blog post about why we need to keep whatsapp open on our smartphone to make it work on our PC.
It said that WhatsApp fetches the data (messages) from our smartphone and shows them on our pc which seems pretty good as it will lower the load on our database.
So now i wanted to know if there is a way to do so in react-native i.e, access the localStorage of one device from another.
Why i want to do that?
I am building an app where in the profile, i also take the profile picture from the user and i don't want to store it on the database but instead store it locally and serve it from there.
The reason for that is that we need buckets to store media files and serve them from there and i wanted to cut that part when deploying my app.

Saving data on phone in a Cordova app

I am making a mobile app using Cordova and I need to save some sensitive and not so sensitive data inside the phone. I am a bit lost on what is the best way to do it.
I need to save:
A JSON web-token (for authentication).
A response from server (I save this to populate my page in case the GET request fails).
Coordinates information when user is logging data to the app (for later upload to a server from with in the app). These will be many separate logs, and can be large in size for local storage ~5-10 MB.
Till now i have been successfully saving everything I need to the local storage but I don't think that is the correct way to do it. So that is why I need some help in deciding what is the best course to take from security point of view.
Saving server response is just for better UI experience and static in size so I guess local storage is a good option to use.
But web-tokens and GPS logs is sensitive information and I dont want to keep it in the local storage as it is accessible from outside the app.
What other options do I have?
Cordova still doesn't have encrypted storage.
Is saving to files a good approach? This here says that data contained inside cordova.file.applicationStorageDirectory is private to the app.So can I use it to save the logs and the token?
The plugin also lists the file systems for Android and iOS and lists which of those are private.
I am currently working with android phones but want to extend the app to iOS later. I have never worked with file systems and caches before so I am a bit lost.

Daily scheduled notifications

I need a bit of guidance, so for my application i'm looking at using local notifications to send a notification every morning at 7 o'clock.
The issue i'm having is how can i make the content for the local notification dynamic mainly the body and the attached image? As it will vary for the user on a daily basis.
What would be the best way to go about this since you can't edit future notifications.
Here's a little lesson about notifications. First, what is a notification? It's basically an alert presented on your behalf by the system. Second, there are two kinds of notification: local and remote.
Let's imagine, then, an app that aims to present a notification to the user every morning at 7 AM saying what the current temperature is outside. (Assume for purposes of the example that we have a way of learning this information.)
A moment's thought will reveal that this cannot be done with local notifications. We cannot know the current temperature at 7 AM until 7 AM (or close to it) and we cannot schedule the notification unless the app is running. Therefore we would need the great good luck to have the app running at 6:59 AM in order for this app to work. But an app only runs when the user summons it, so that is extremely unlikely.
Therefore a task of this kind is possible only with remote notifications. A remote server is always running; therefore it can get the temperature and "ping" the user's device at 7 AM, and the system will present the notification on your behalf. You would therefore need to possess such a server in order to write the imagined app.
(An alternative using local notifications would be this: You schedule, say, a week's worth of local notification in advance. Then if you have the great good luck to find the app running before a notification is presented, you tear down all the scheduled notifications and do it again with a more up-to-date forecast. But of course this cannot possibly work as well as using remote notifications, and it will stop working entirely after a week if the user doesn't launch the app. That, to put it bluntly, sounds pretty lame.)

iCloud update policy on CoreData in development and production mode

as specified in the subject I am developing un application that made use of CoreData and its built in iCloud feature.
Whenever I connect my devices (an iPad and an iPhone) to xcode and run the application, I can see after few minutes the log of CoreData updating data. In both devices on iCloud section, I can see my application full entitled for iCloud.
First question: how often are iCloud content pushed (or pulled) to devices ? Is there a way to force this ?
Second, I noticed that the devices, once disconnected from Xcode, doesn't update their data anymore.
While if plugged again and run the app, the data get updated.
So the question, is iCloud in development mode (the app is not yet submitted to App Store) working only trough xcode, and if yes why ?
Or am I doing something wrong within the objective-c code itself ?
thanks
The updates are getting pushed to iCloud servers whenever you save CoreData moc.
The updates are getting pushed from iCloud to device automatically. There's no way to force it as far as I know.
It can take from seconds to dozens of minutes for an update from iCloud to appear on your device (also depends on your connection speed). Apple doesn't give any guarantees on when it will actually happen.
I haven't seen any difference in iCloud behavior whether app started fom XCode or not. Check your code and UI updates base on iCloud pushes.