iCloud + Core Data today (10th july 2015)
Asked Answered
S

1

8

Some years ago Apple released the iCloud sync of Core Data apps. Then I released an app for iPad / iPhone / MAC with a shared model using the new Apple mechanism. Things have not gone as expected. The sync mechanism sometimes doesn't work. For example, the last months, in my case, rarely the sync ends well through my 3 devices. Uploading objects in general works fine. But the download process of new or deleted objects normaly crash. Apple released some time ago a way to force the devices to redownload ALL the objects of the model (NSPersistentStoreRebuildFromUbiquitousContentOption) that normaly works but it's not an acceptable solution.

My questions: Does someone have accomplished to get iCloud + Core Data working fine? What about running iCloud+CD under iOS 9 + El capitan, any experience?

I'm evaluating migrate to new CloudKIT API but I don't like the idea of self-manage an object upload if the device is offline. Does the new mechanism of push notifications indicating model changes works fine?

Thanks

Stoppage answered 10/7, 2015 at 2:22 Comment(3)
iOS 9 is still in beta, specification will probably change day by day... it's working today does not imply it will work tomorrow, until the first GM is released.Gooseberry
I think you haven't answered any of my questionsStoppage
my idea is: I think nobody will give definitive answer, as the given answer may become invalid in any minute.Gooseberry
S
8

This is a question I've researched deeply in these last few months, I'm afraid without a definitive answer.

Here's what I can tell you from my experience:

  1. If you, like me, don't want to start over with CloudKit, which works reliably but requires you to manually handle much of the syncing work and the conversion of CKRecords to ManagedObjects, give Ensembles.io a chance: it's working very well for me, the layer between Core Data and iCloud really is working in my case, where CD+iCloud didn't. I'm using the 1.0 version, which is open source and supports iCloud as one of its possible backends (and version 2.0, paid, supports even more); in a few days I got reliable sync with automatic de-duplication (you have to provide a uniqueIdentifier property to have it working, but I already had something in place...). The only issue I haven't figured out yet: sometimes (1 case out of 10, I'd say) an object doesn't sync right away when edited or deleted, but it always get on the other device when another object is added, edited or deleted: nothing got lost and everything has been handled "automatically" for me when these delays occurred, but still, I'd prefer that everything always synced right away. Ensembles also has a good logging for debug, something you'll appreciate coming from vanilla CD+iCloud. If you want to give it a try, you should take a look at these resources:

  2. If you are absolutely sure that you don't want layers / third party code between Core Data and iCloud (I thought so myself, but I've changed my mind when I realized I lost three months of my life and got nothing in return), the implementations of Core Data + iCloud that I've found online and looked more promising were these:

I haven't tried myself these last two solutions, because my last plan of attack was to try Ensembles and, if it didn't work for me, go with those approaches. Since Ensembles has been very good to me, I didn't need to try them, but again they looked solid.

One last thought that bothers me: in 2015 WWDC sessions there's no mention of Core Data + iCloud. This, to me, spells doom for the syncing solution we're choosing.

Hope this helps.

Sidky answered 10/7, 2015 at 17:10 Comment(21)
+1 Ensembles. Personally, I gave up on Apple's iCloud integration with Core Data a long time ago. It could be fixed by now, and there are some who claim it has been... but I went to a simple solution that I rolled myself, and then to ensembles and have never looked back. This is one of the few areas where I feel Apple has completely failed, and I just completely abandoned the technology entirely. Ensembles is so good at what it does, you will WANT to pay for the upgrade, even though you may have no need for it.Jovi
@JodyHagins I completely agree with you; I've read many times that now the situation is better, but still, I haven't found a really viable solution to make it work: if now is not that bad, I can only imagine how awful it worked a few years ago ;). I confirm that the desire to purchase Ensembles 2.0, or at least pay for the book is strong just to support them, but I'm working on a hobby app on a very tight (non-existing) budget, so I have to wait to sell a few copies first when I'll launch.Sidky
Hi cdf1982, as far as I know the problem with 3rd party sync alternatives is that they can't operate in case the device is offline and eventually it get connection with a WIFI net. That is: imagine that you are offline and you add an object. Then you put your mobile in your pocket and when you're walking it connects to a WIFI. With iCloud + CD I think there's a system queue that will upload your object automatically. But 3rd party sync mechanisms can't. It is true?Stoppage
Ensembles isn't actually a 3d party sync mechanism, because it actually uses iCloud (or Dropbox, or others) to sync data. In my case, I add an object on device 1 and it syncs with the cloud (Ensembles with iCloud as backend) right away; if device 2 is offline, it will get the item as soon as it is online (of course, "right away" and "as soon" is usually seconds, but sometimes it takes a bit more, you have no control of that with iCloud). If I understand, your concern is that your app wouldn't load the synced data received from the cloud while inactive, right?Sidky
That, would actually be a problem also if you used "plain" Core Data + iCloud: iCloud sync doesn't automatically "wake" your app while in background to merge the sync. So, doesn't Ensembles.Sidky
I'm trying myself to find a way to "wake up" my app to sync data received from the cloud while the app is in background. My understanding is that the best way would be push notifications, but that is not an option for me (I have no server). I'm experimenting with background modes and application.setMinimumBackgroundFetchInterval(), but I think that someone also uses geofencing as a technique to "wake" di app periodically and work around the limitation of the OS. I'm no expert in this area, I'm trying to make it work myself...Sidky
Hi cdf1982, I'm referring when you try to add an object while the device is offline (the sender, not the receiver) and app enters in background without connection, and 30 mins later the device gets a WIFI but you don't open your app. iCloud + CD will delegate to iOS core to automatically upload the object. Does Ensembles solve this case?Stoppage
I'm pretty sure the answer is yes, because Ensembles uses Core Data + iCloud, but since I'm testing just these cases with my app right now, I'll do this: 1) Launch my app on iPad and my iPhone 2) Put my iPad in Airplane mode 3) Create a new object on iPad while in airplane mode 4) Hit home button on both devices, iPad and iPhone, to send the app in background 5) Wait 30 minutes, then disable Airplane mode on iPad without returning the app in foreground 6) Wait a few minutes, then check on the iPhone if the object created on iPad arrived. I'll let you know in an hour how it goes...Sidky
Hi cdf1982, thanks a lot, that's exactly what I was referring.Stoppage
Well, I have good news, but now as good as I would have hoped: I put my iPad in Airplane mode, created two objects, hit home button on both devices and walked away from my computer. When I returned, I turned off Airplane mode on iPad WITHOUT returning the app in foreground. I waited 10 minutes, then I resumed my app on the iPhone and, a few second later, both objects created on iPad while offline appeared. So, for what you asked, it's a success. For what I want, not yet: the phone did receive data from iPad when it returned online, but didn't merge them while in background, that is the...Sidky
... behavior I want: I want the user to find the app already updated when it returns in foreground. Good thing is, while I was away my phone logged 3 times "UPDATE IN BG", so Background Modes and application.setMinimumBackgroundFetchInterval() seem to do something, they just don't start the merge of data received from iCloud automatically yet. And I'm not sure I can make this work, because you have little time while waking up to perform actions. But still, iPad sent app data to iCloud as soon when the connection returned available, without needing to run the app. A big win for Ensembles!Sidky
Ok thanks for the info. I think I'll investigate what about new CloudKit API, zones, subscriptions, etc. I think CK API can upload UI in background with silent push notifications that "wake up" the app. I'm also waiting for experiencies of iCloud + CD in iOS 9 + El CapitánStoppage
No problem. One last update: adding the call to merge Ensembles data inside application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void), the method called by iOS automatically for BG operations when Background fetch is enabled, I'm now able to have data from other devices already handled, and local UI updated, when the app returns in foreground. Of course you can have those with CK notifications too, but you have to handle yourself conversion from CK Record to NSManagedObjects and find a way to deduplicate. Lots of code...Sidky
... And, regarding experiences of Core Data + iCloud with iOS 9 / El Capitan, don't expect anything new: I've watched every WWDC 2015 relevant session, and I heard no mention, not once, of CD + iCloud. Apple is letting this go, not that they're going to deprecate it, but think of this: for the new Notes app they didn't use CD + iCloud, that was the logical solution considering that you need your notes also when offline: they used CloudKit and created all the required code for local handling, sync, deduplication and so on. If Apple doesn't believe in plain CD+iCloud, why should you?Sidky
After trying to implement new CloudKit API in my CoreData App I have realized that this new API is not easyly combinable (at least now) with a complex CD model. I think I'll spend 1 or 2 days to test Ensembles. There's something that I'm not understanding: how it's possible that Ensembles supposedly works if it uses the broken Apple CD + iCloud mechanism? Tomorrow I'll start to read about it... :-)Stoppage
Yeah, combining CK with Core Data is doable, but requires lots of code, as I was saying my answer and yesterday. Basically, Ensembles works because it uses iCloud as a dumb pipe to sync data: for Ensembles there's no difference if you use iCloud, Dropbox, or a custom sync server, those are backends that Ensembles uses to get data from one device to the other, then when data is there, it automatically handles all the operations (integrating data, deduplication, baselining). I'm sure Ensembles book will explain this way better than me. Good luck!Sidky
I have read the first 30 pages of Ensembles Book and looks absolutely great. Now I understand the mechanism and the meaning of wonderfull concepts in sync world as backend agnostic. Thanks cdf1982, I think Ensembles is more interesting that waiting to if some day Apple's iCloud + CD sync mechanism finally works.Stoppage
Has anyone tried the Seam framework to convert between CoreData and CloudKit? github.com/nofelmahmood/SeamSeclusive
@Koen I had no idea a solution like this already existed, I'm really curious to read some feedback from users, thanks!Sidky
@cdf1982: I haven't gotten it to work yet, since it is in Swift and my project is in ObjectiveC. Also, it looks like the upcoming book on Core Data by Tim Roadley will have a chapter on CloudKit: safaribooksonline.com/library/view/learning-core-data/…Seclusive
i am a first time developer and making my first app. spend two years on the app (on and off the project) and i am glad it isn't me who is wrong over here but rather apple is. i should had researched first as i lost 9 months of my life trying to fix this damn thing. came accross ensembles too. planning to use it. doe anyone know of a good video tutoring how to use it?Limacine

© 2022 - 2024 — McMap. All rights reserved.