Apple Watch and Core Data iCloud Syncing - ios

I've been wrestling with this problem for a while now and can't seem to find anything on the topic. There are plenty of example on how to use Core Data with Apple Watch, but none that have iCloud support, so I'm hoping someone can shed some light on this subject.
I have an app that uses Core Data and the NSPersistentStoreUbiquitousContentNameKey option when creating the persistent store to make it sync to iCloud and between devices. This works great between iPhone and iPad. However, Apple Watch syncing is spotty at best. Sometimes it works, sometimes not and when it does an initial sync it doesn't seem to sync anything after that.
The reason as I see it is, even though the Watch app is an extension of the iPhone app, because it uses NSPersistentStoreUbiquitousContentNameKey it created a database under another folder instead of using the same one as the iPhone.
Path for the database for the iPhone
iPhone:
PFUbiquitySetupContainerIdentifierKey: 8B652C84-62E1-4FFF-B822-13C90C19DC5F
file:///Users/Aaron/Library/Developer/CoreSimulator/Devices/
6E2EE13A-3148-4693-A92F-7B23C709CF78/data/Containers/Shared/AppGroup/
B7DEE2C6-D345-460C-884D-74C011BF98BA/CoreDataUbiquitySupport/
nobody~simA7716E1E-6B21-5DAA-AF81-CAF6AE77A985/iCloudDATA/
8B652C84-62E1-4FFF-B822-13C90C19DC5F/store/appname)
Path for the database for the Apple Watch
Watch:
PFUbiquitySetupContainerIdentifierKey: 44220421-4F7C-46AC-B3D7-6ABEDA62C77B
file:///Users/Aaron/Library/Developer/CoreSimulator/Devices/
6E2EE13A-3148-4693-A92F-7B23C709CF78/data/Containers/Shared/AppGroup/
B7DEE2C6-D345-460C-884D-74C011BF98BA/CoreDataUbiquitySupport/
nobody~simA7716E1E-6B21-5DAA-AF81-CAF6AE77A985/iCloudDATA/
44220421-4F7C-46AC-B3D7-6ABEDA62C77B/store/appname)
Notice that the tile paths are exactly the same with exception to the PFUbiquitySetupContainerIdentifierKey that is in the paths, each device has their own.
If I remove the store option PFUbiquitySetupContainerIdentifierKey, it works fine, but then the devices won't sync with each other.
I was wondering if anyone else has had this problem or any possible solution to it, or am I just doing it all wrong?
Thanks in advance!
EDIT: After contacting Apple dev support, it seems this is a known issue with Core Data & iCloud syncing when the code for that is in an extension. I had to move all my data handling back to the main bundle for it to work again.
Link to known issue thread: https://devforums.apple.com/message/1051015#1051015

Related

Save my app data through phone restore - iOS Swift

What technology is available for me to save app data (Core Data) so that when a user switched phones and has a fresh phone they still have my app information?
I have heard people say cloudKit, however that seems overkill, I don't want the functionality to handoff between devices. I simply want the app data to be backed up when the user has iCloud backup enabled for the app.
If the answer is iCloud, what steps must I follow to enable such behavior? I have seen links to cloudKit tutorials, however all of them cover an extensive amount of material and work, and what I want to achieve seems simple enough. (As stated before I do not want handoff between devices, I have no need for it.).
Thank you for the suggestions
Edit: I have gone into "capabilities" and switched on iCloud. Now in my iPhone settings under iCloud backup my app appears. However does simply switching on iCloud capabilities persist your app data to iCloud Drive or backup?
Ex: Go to Settings -> Account -> iCloud -> Scroll down to iCloud Drive and ensure it is checked. My app shows there, but I haven't included any code to implement this (I simply flipped the switch in capabilities)
So from my current understanding I create Core Data as normal, however I add a listener for an changes/updates/deletes/creation of the persistentStore and upload/update iCloud via document storage when this happens? I was under the impression that iCloud was being depreciated and that only cloudKit will remain?
Edit 3:
iCloud = Depreciated, therefore the one and only solution available is cloudKit.
How does one implement cloudKit to interact correctly with Core Data?
What comes to mind is the following:
- Enable cloudKit
Listen to all changes to persistent Store and reflect these changes in CloudKit (somehow)
On app start check to see if CloudKit matches core data (somehow)
if they are not the same then download cloudKit data and save to core data (somehow)
Please clarify if this is not the correct route to proceed down
What technology is available for me to save app data (Core Data) so that when a user switched phones and has a fresh phone they still have my app information?
There isn't any such problem. I've upgraded devices many times and nothing has ever been lost. When I got a new iPhone last year, and when I got a new iPad this year, all my apps on the new device simply showed up with the same settings and data as before on the old device.
That's the because the user who gets a new device backs up from the old device and restores to the new device. That's what I always do and it just works. You cannot reliably work around a user who is too stupid to know to do that, so I recommend not even thinking about it.

How to access iCloud as core data for iCloud is deprecated

I am working on an app using core data as I thought I could use sync functionality provided by core data for iCloud for syncing data between iPad and iPhone, now it is deprecated.
I came across CloudKit but it is mainly to create and manage data that can be consumed by community of end users but not just one like chat apps. My app is only for single user so that user can sync their data between their apple eco system devices.
I am new to iOS development so it would be helpful to know which technology will meet my requirements.
In short, can you please suggest what should I use
If have to store data on phone for offline use
Then sync it across devices if user have say iPhone, iPad, and iWatch
P.S. Can you please suggest something that doesn't need the user to be signed into iCloud while saving the data.
The CoreData/iCloud integration is deprecated in iOS 10 / macOS Sierra, and there is no replacement (at least so far).
Way forward so far :
Use third party framework such as Ensembles : http://www.ensembles.io
Use CloudKit, which can be used for that. See this for a help : https://nickharris.wordpress.com/2016/02/09/cloudkit-core-data-nsoperations-introduction/
Note that CoreData/iCloud integration will still be working for some time in the future. Apps that are using it will not suddenly stop working.
Apple just released a new container for Core Data that can sync across devices with iCloud.
Get started here: https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/setting_up_core_data_with_cloudkit
Note, this requires iOS 13.
You should take a look at the videos from the latest WWDC.
Here is something about CloudKit Best Practices and whats new in cloud kit
CloudKit really have some nice out of the box solutions and you dont have to bother about security. Also the newest functionality enables you to use notifications for synchronising apps using the same apple id and even beyond, as the user can now opt to share certain data.
Depending how much data you are syncing, NSUbiquitousKeyValueStore.default() is also an option. But only for very less data

Core data with iCloud.. seems like they have two storage

I made app for iOS and Mac with core data with iCloud.
Synchronizing with any device is working great until iOS8 came out.
since iOS8 came out, in XCode and provisioning portal, I had to put one more ubiquaty key like "iCloud.xxx.xxx" with my previous one "XMKH8e3SK.xxx.xxx" at the same time. I didn't want to make any change to my app but I had to, because once Apple migrate my apps's data to iCloud drive, data is gone and it's restored when I put that key(iCloud prefix "iCloud.xxx.xx" in XCode and provisionaing portal).
It seems OK after that.
but now I have really serious problem.
MY IPAD(iOS8) ~ MY MACBOOK AIR(Yosemite) is syncing each other and MY IPHONE 6(iOS8) ~ MY IMAC(Yosemite) is syncing each othere BUT NOT ALL DEVICES!!
It's too weird. Can not believe this is happening. I hope not my customers are facing this issue. but They are!
How can i solver this issue?
Thanks.

SharedCoreData using OSX app and iOS App

I am using the source code from https://github.com/jab5990/TestCDiCloud.git. Apparently this source code is the sample code from the WWDC 2012 Session #227 called Using iCloud with Core Data. The original source code does not seem to be available on Apple's website any longer.
I am new to Core Data and I struggle quite a bit with these concepts. The situation is that the data does not get refreshed while both apps are running:
I add data to my mac app (2 first rows)
I start the iPhone 7 app in the simulator (after signing in to iCloud)
The entries from the mac show up
I add more data to the mac app
The data does not show up after several minutes
So on in DetailViewController.m:204 on the iPhone App the Notification NSPersistentStoreDidImportUbiquitousContentChangesNotification is registered, which should mean that changes from the Mac app should be incorporated.
How do I ensure that the data stays in sync.
In the simulator sometimes you need to use the Debug -> Trigger iCloud Sync menu to trigger the sync. It is not automatic like on iOS devices.
If you keep having issues then try these sample apps which include code for handling iCloud account switches, moving from local to iCloud, making backups, storing and fetching backup files in iCloud, etc...
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/
BTW that wwdc2012 session is old so I would look at the more recent wwdc2013 session 207 video for things that have changed in iOS7, OSX 10.9
iCloud can take quite some time to transfer files, and then the Core Data framework has to get around to actually importing the files. So several minutes or even longer is not unheard of for a delay.
You can use Xcode to trigger a sync with the Debug menu. That may help. Otherwise, you probably just have to wait, or perhaps quit and relaunch the app.
Welcome to the wonderful world of iCloud debugging.

Questions concerning iCloud + core data

I have an app on the app store, that uses coredata as storage. I wan't to update the app with iCloud synchronization as new feature. Following apple`s sample code, I managed to have my core data storage synchronize between devices.
However, I'm experiencing problems when either iCloud synchronization is turned off/on in the app on only one of the devices, or when the app is deleted from the device and the reinstalled. In both cases, data is not synchronized back to the device, although it is available just fine on a second device (which was not disabled/reinstalled).
I also found that all storage is effectively erased completely, when I delete the app from all devices, and then reinstall. Althrough I get a couple of merge notifications in the console (even some without errors), I can't see no data in the local storage of the device.
Browsing the mobile documents folders on my mac still reveals lots of transaction logs in the icloud storage of my app.
Even deleting the app from all devices and starting from scratch wont sort things out. I will end up in a situation where data is either only synced to one device, or not synced at all.
I wonder if there is anything I can do about this inconsistent state that is created when only one device is temporarily iCloud disabled, or the app is deleted from ONE device?
As for my code, its an 1:1 copy of the recipces example from apple.
Daniel Pasco talked about using Core Data and iCloud together at NSConference 2012. Some notes from that blog post:
launching with -com.apple.coredata.ubiquity.logLevel 3 to get a spamfest in the message log saying what Core Data and iCloud are doing.
The conclusion from this talk appears to be that using Core Data and iCloud are really not ready for each other at this stage.
He posted an updated Core Data Recipes project on Github which may or may not fix your problem.
Apple makes it seem easy, but there are a number of nuances with regard to correctly seeding iCloud with data, and what happens afterwards when iCloud support is toggled on and off on different devices.
I implemented a sample project that demonstrates a straightforward way to add iCloud support to Library-style CoreData apps. It's called iCloudStoreManager and it's available on github.
I'm still testing it before I add iCloud support to one of my own production apps. It's working, but I see unexpected errors and delays when an iPad 3 is in the mix. It works, but with long delays.
I've also tested with iPhone 4, iPhone 4S, and the original iPad, and any mix of those devices works well in my experience.

Resources