CoreData not storing data across multiple iOS devices - ios

I am trying to build an iOS application using Objective C.
Where say some user is logged into my App as UserName A and they want to invite for a particular task, another user of my App ,B using their FirstName/LastName/Phone Number.
I am using Coredata to store the user credentials when they register.
The issue is : This works fine when I register both A and B from the same iOS device but NOT when I register A from iOS device 1 and B from iOS device2.
Coredata seems to store only the user data in the local device.
How can I ensure my app works for all users logged in form any iOS device ?

Core Data does not upload data to any server or synchronize data across devices. It's designed as a local-only data store. You can turn on iCloud syncing in Core Data, but this has been deprecated as of iOS 10.
If you want your app data to be available across multiple devices, you'll need to write some code to do that. Apple provides CloudKit, and there are many third party solutions. But you can't just tell Core Data to sync data, because Core Data doesn't do that.

Related

Retrieve user defaults information from an Apple Watch

I have an iPhone and an Apple Watch paired together, and there are some values stored in the user defaults of the phone. I want my Apple Watch to be able to retrieve that stored information somehow and bring it back to the Watch. What is the best method of achieving this with Swift?
Since watchOS2, you don't have any built in function for communicating between the iOS and watchOS app other than the WatchConnectivity framework. Due to the fact that Watch apps are no longer considered App Extensions, they don't have access to AppGroups and hence to UserDefaults on the iPhone.
For syncing UserDefaults, the updateApplicationContext(_:) function seems to be the best solution. You can send a dictionary of data with this function (the data you just saved to UserDefaults on the iPhone) and the system tries to make sure that the data is received by the time your app is displayed to the user. If the function is called several times before the app would be visible to the user (run in the foreground), the system overwrites the previous data, so the Watch app only receives the most recent data to display.

Apple watch - Storing data to Apple Watch separately without dependence on iPhone

I am working on an app which consists of an Apple Watch component. Once the iPhone app is launched, it will send information (device id, IP address, etc.) to Apple Watch via the WatchConnectivity framework and data will be stored to user defaults. The Apple Watch will use the information to send data to the server. But in some scenarios, the values from NSUserDefaults returns as "null".
For example
When the watch is switched on and off.
When the app is killed and not opened etc.
Please give some suggestions for storing data on Apple Watch permanently and without being dependent on iPhone every time.
I am storing stuff in the watch's filesystem by serializing/deserializing my custom objects by implementing NSCoding and using NSKeyedArchiver and NSKeyedUnarchiver.
Depending on the complexity of the data you want to store, it might be worth using a database framework (i.e. Realm or CoreData) to store persistent data on the watch.

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.

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

syncing CloudKit and iCloud

I have an app which currently syncs via iCloud, so the user can use the app on multiple iOS devices and has the same data everywhere.
Now I'd like to switch to CloudKit; but how is this possible if the user e.g. only updates on one device? So it's the same user, one time with CloudKit, one time still with iCloud... I guess syncing isn't possible then if I get it right?
just check if there is data in the private container. If not, then migrate and sync the data from iCloud to CloudKit. Then if he opens the app on another device then the data is already migrated to CloudKit and you can just sync that data.

Resources