What is considered too heavy for UserDefaults? - ios

I'm building a simple iOS app that will be the first I'll have put on Apple's App Store. At one point, the app accesses the user's contact list and lets them select any number of contacts they want to save as favorites.
For ease of building version one, I am currently using UserDefaults to save these favorites. While it has worked well in my limited testing, I know that Core Data and CloudKit are stable options for larger solutions.
For an app like mine, where I'm only using UserDefaults to save a select number of contacts as favorites, is UserDefaults an adequate solution? Or should I transition to something more robust like Core Data or CloudKit? There is no limit on the number of contacts a user could select as a favorite, so there is the edge-case possibility of a user selecting all of their contacts one by one and attempting to save them all as favorites.
If the user gets a new phone and loses all existing data due to UserDefaults being local on the device, it would not take long to get this app back to where they previously had it.

You can use CoreData to store favorite contact locally for large data as per your concern. Now when user switch device or delete app then all data will removed. So for that you can sync with cloudKit or other option is backend server. When user add any contact as favourite same time that contact will add to Core data as well as in backend server. You can sync this backend server data when user login first time in the app, then you no need to sync again. other all thing are as per requirement.

Related

Swift & Firebase - Database and UserDefaults

I'm new to Swift & Firebase and I haven't really figured it out yet. Currently I'm thinking about adding a Database to store data for the user to fetch in case he switches device.
So far I've stored data with UserDefaults. Now that I'll probably be using a Database, do I still need UserDefaults? What if the user is not able to get internet connection and closes the app, before he gets to synchronize his data going online - will there be a danger of data loss?
Or is it best to keep both - UserDefaults and the Database?
A UserDefaults from Apple documentation:
An interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app. https://developer.apple.com/documentation/foundation/userdefaults
UserDefaults is a great solution for keeping unique values between app sessions. For example user settings, preferences (Simple, dummy data).
To share data between 2 devices you can use Firebase Storage. Another option is a CloudKit.
Synchronisation - this is more about what you need and how you would like to handle it. You can base on online data, so there would not be need to synchronise (you are always up to date). Or make periodically synchronisation, but you would need to think about best solution for local database.
It depends on what you are doing. Using user defaults is certainly fine for holding defaults but not information. What if 2 people use the same device, then you have user defaults for one user and a database for the other. I personally would use just one. If you are learning Firebase I would suggest 2 tutorials:
https://www.youtube.com/watch?v=F42CzqIBxHQ&t=406s
https://www.youtube.com/watch?v=RMudKhNY0sI&t=627s
and the question I just had answered about reading and writing to a secure database: User already authenticated in app and Firebase realtime database returns failed: permission_denied

Page loading with Firebase

I use firebase in my ios app. For example, my database stores some data sets, which has being created by user. When user is authenticated, application starts downloading. But it is okay, when user has not bigger count of sets, because in this case, it is easy to download all data sets. On the other hand, if user has many-many data sets - it is terrible, because my app will download ALL this data immediately. How to use "page loading" with Firebase? For example - if user scroll to bottom, application starts downloading new piece of data from firebase database.

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

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.

Pushing data to all devices when database changes with Realm Swift

I know that Realm offers two way syncing and can trigger server-side events when data changes.
I am making a sort of messaging app, my question is how would I implement it so that when someone writes a message (which will update the DB), how does this message immediately display on other devices?
All you have to do is to open the same Realm on all devices, then they will all see the changes instantly as they happen (as long as they are connected of course :-)).
Check out the tutorial, it shows the data being realtime synced between both an iOS app, a Mac app and the Realm Browser: https://realm.io/docs/tutorials/realmtasks/
I'm one of the developers working on Realm Tasks, incorporating the latest features of the Realm Mobile Platform into it. :)
Making a chat app has become significantly easier with the release of client Realm sharing in Realm 2.3.
To make a chat app, the logic flow would be something like this:
A user starts a new chat room. This would be represented in RMP as a single, private Realm file belonging to that user.
The user can then invite other users access to that chat room via the sharing mechanism.
Every user can then contribute messages. Each message would be a separate Realm Object written to the shared Realm.
Each time a new chat message object is written to the Realm, it would be synchronized with every other user in the room.
Please let me know if you need any additional clarification.

Checking if any contact in contact app has been changes (Swift)

Right now I build an app that provide sync contacts. For that, every time the app comeback to foreground, I check all contacts(CNContact) and compare to my contact in app's core data and detect if any contact that different and merge the data. It works okay but it takes a lot of memory.
Is that any way to detect if any contact in contact app has changed without comparing all contact?

Resources