Share database [core data] between different devices, not just the user's ones - ios

What is the best way, to share a database between different devices, that are not just the user’s ones, but for example could be his friend’s phone. That means that iCloud is not an option.
Example:
 All of my data is app-user specific, so basically:
user logs into my app, do some work
then he can log in with the same acc on his friend phone and data should be the same
Is there an any way to upload the whole user specific database to some online storage provider (like firebase,… ) and then download it on another device and initialise core data stack, when the same user logs in on a different device?
Or is it the only way to sync data with the server and than preload the database?

You could simply upload the whole database file(s) and then download it on another device. The problem though is portability. You need to ensure that both devices support the same version of the database so they are compatible. To port the same thing to another platform is again a different story but doable when not using core data.
Then there is a problem of conflicts. Imagine you forget to log out from the second device and you open it after a week and the database is accidentally synced back to the server. This will make you lose all the data you created on your "main" device.
So in general it is possible to sync the whole thing but you will have loads of issues. You should create a server that supports all entities and works through ids (so you know the object was modified and not created) and date modified to be able to resolve conflicts.

Syncing data between multiple devices is the biggest reason to use something like Firebase. That's one of its primary purposes. You would use Firebase for data storage instead of Core Data, and it would automatically handle syncing between devices. You don't write code to upload or download anything, you just read and write Firebase data and it handles the syncing. It supports user accounts, so if a user logs on on a different device, their data automatically syncs to that device. There are numerous other options besides Firebase, of course.
CloudKit also syncs between different devices, but it's linked to the current iCloud account on the phone. Since you want in-app login, it's not so good.

Related

Storing a text file in iCloud in Xamarin

I need some clarification and Microsoft documentation is only confusing me more.
I want to save a txt file in iCloud so the user doesn't loose some data that belongs to them.
This db document is some information I am retrieving from a local database and storing in a text file. I have seen two ways of doing this.. however all the posts on this topic are very outdated and I don't know which way might be best or if they are even doing what I trying to do.
All I would like is to be able to have the user backup this particular file to their iCloud account, so they can still keep this info even if they change phones or delete the app and want to restore from iCloud.
Microsoft's documentation points me to this page https://learn.microsoft.com/en-us/xamarin/ios/data-cloud/introduction-to-icloud
I began setting up the provisioning profiles and setting the iCloud options on the entitlements page etc. However the documentation when sideways for me when they began creating a monkey page UI Document and having the user manager the ubiquity documents (which I don't want) I actually don't want the user even seeing this Txt file. However this option shows how to check if the iCloud is even turned on on this user's phone.
Now this other option I think is more straight forward and I read the documentation on it here https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system using something like this to store and retrieve a document:
var libraryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "..", "Library");
However what if iCloud is turned off? Is this an automatic backup option?
Any type of explanation is helpful.
The iCloud storage API in iOS 5 allows applications to save user documents and application-specific data to a central location and access those items from all the user's devices.
About the definition of iCloud, you will know that it will save your data to cloud server. And it can be used in all the user's device if turn on the iCloud.
However what if iCloud is turned off?Is this an automatic backup option?
Therefore, if iCloud is turned off, you could backup the data in device although it will not be used for other user's devices. You could save it in Application directories. This should be a good chooice to backup your data. And you also can get the data when you need them.
In addition, you also can use other cloud server APIs to backup your data. Such as Azure Storage, Firebase Storage etc.

When using NSPersistentCloudKitContainer: what to do if the user logs out of iCloud?

From several Apple WWDC talks on CloudKit, when using CloudKit to sync private user data across devices, when the user logs out of iCloud, the App is supposed to empty the on-device cache / local replica (I use Core Data for on-device permanence). Then when this user (or any user really) logs back into his respective iCloud account on that same device, the device is then supposed to sync back down the data from the corresponding iCloud account. Makes sense!
I was wondering how I can achieve this when I use NSPersistentCloudKitContainer (instead of writing all that CloudKit code myself). I looked at the sample code related to Apple WWDC 2019 session 202 ("Using Core Data with CloudKit"), and this code does not what I want (the sample just does not focus on the iCloud account part, that is why they do not bother to empty the cache, I think): Indeed the NSPersistentCloudKitContainer stops synching if I log out the user, but it picks up again synching when I log back in. That is actually more like the desired (and indeed also actual, which is good) behaviour by the sample App when my web connection goes offline for a while.
But if the user logs out of iCloud (I use CloudKit framework to be informed about user account logout and login status), what I would need to do is somehow:
"disconnect" the sqlite store from the NSPersistentCloudKitContainer, then
empty (or even delete) the database, and then,
when the old (or another) user logs in, initialise a new NSPersistentCloudKitContainer (that will recreate an empty database as if the App would be used for the first time ever), so communication with the respective Cloud database can proceed.
I can not just empty the database WHILE being "connected" to the NSPersistentCloudKitContainer, as it records all the deletes and as soon as the (same) user logs in again, these deletes are synched to the Cloud, which I of course do NOT want.
Is this how one does it, aka essentially follow these three steps?
And if yes, how do I do step 2? Since I must use some kind of NSPersistentContainer to talk to the database, Apple warns not to directly do file system operations but only via Core Data. So do I need to init a "standard" NSPersistentContainer (without a communication pipe to the Cloud), and then destroy the database (e.g. using e.g. the coordinator instance method destroyPersistentStore())?
Or is there another approach for achieving the same thing?
Thank you very much for any help!!

How to persist core data in cloud without authentication

I am designing an app using core data which has a local Cache. I am thinking of using CloudKit for syncing it across multiple iOS devices. Since CloudKit is not for persisting data, I am pretty sure that if user loses their phone that data is gone forever. My app doesn't require any authentication so how do I save the data in cloud along with local Cache so that it can be sync across multiple iOS devices like iPad or iWatch
P.S. Is it possible to save user generated content files in Documents so that they can get backed up by iCloud automatically without using CloudKit?
The data is backed up on iTunes and iCloud periodically, so if your user changes the device then they can restore the data from there.Yes it is possible to save user generated content files in Documents so that they can get backed up by iCloud automatically without using CloudKit
As per your problem , Only creating Coredata enables you to manage data across different devices but two important question need to understand before this :
How can you preload existing data into the SQLite database?
How can you use an existing SQLite database in Xcode project?
Here is one of hot favourite tutorial for managing this :
https://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration

How do I cache Firebase data for offline usage?

I build apps that bundle up JSON data. I want to switch to Firebase as my backend, but I need to ensure I can access the data even if firebase is offline. There's no guarantee that the user will have an internet connection at the time they launch the app. The data consists of a fairly large JSON blob.
I heard that firebase does cache data on iOS for offline access, and that's great. I just need to know how to bundle the data for that first time the app is ran, so the user can use the app prior to getting to a network connection.
As it sounds like you discovered, the Firebase Obj-C client does have beta support for offline access / disk persistence. Details can be found here.
But that doesn't address your desire to "seed" the app with initial data so that it has data available before the app has ever been able to connect to Firebase. Unfortunately, there's no direct support for that.
One hacky solution you could attempt with Firebase is to just do a setValue with the data in question, in order to seed the cache. This should work but will eventually try to write that data to Firebase, when the app gets connected, so you'd probably want to have security rules to prevent the user from actually modifying that data. As I said, it'd be a hacky solution.
For now it might be best to just handle this with special logic in your app that pulls data from some other data source (hardcoded values or an embedded file or whatever) until the first time you get data from Firebase.
Sorry there's no direct support for this at the moment. We'll take a look to see if we could support this more directly in the future.

Packing IOS App with Large Amount of Data

How can I have the customer of an IOS app select and change selection data for use with an app?
I have an app that is designed to operate away from wifi. As such, it needs to carry its data around with it. This makes the data requirements rather large.
A base package is 25MB of data. However, it is likely people who use this app would want as much as 1GB of data.
Is it possible to do selective data downloads and updates from the app store?
How else could this be done
How can I have the customer of an iOS app select and change selection of data for use with an app?
Hard to say. It's not clear what you're talking about. Downloading data through an app is a well researched topic. You shouldn't have any trouble finding examples for downloading data to an app, here or elsewhere.
Is it possible to do selective data downloads and updates from the app store? How else could this be done?
Again, its not 100% clear what you mean. I don't think you can use the app store to provide new data to users who downloaded your app, unless you're talking about a full app update that comes bundled with your new data set.
I worked on a project once that had to work offline, but also work with a very large data set as well as some mapping components. We required users to first download our app, then after authenticating, the app would automatically download a cache of data while connected to a WiFi hotspot. When the user was out of WiFi range the app would operate off the data set they downloaded and would operate normally. When they came back into range it would ping the server to find out if their data was stale. If it was stale it would download new data. The WiFi connection requirement ensured that they would have an optimal experience fetching the latest data.
My thought is to create a menu in your app that lets the user select which "packages" of data they want to save to their device and run those downloads in the background when the user has an internet connection.

Resources