1

To further my basic learning, I've read quite a lot over the past few days on adding iCloud back-up and/or more advanced syncing features to an existing app which uses UIManagedDocument to get its Core Data stack. I'm seeing that some people are referring to updates in iOS 7 that make many of the articles (and Apple's own documentation) from 2011 and 2012 (which seem to be the most common dates on most of what's written [discovered via google searching]) somewhat "obsolete". In general, where does adding iCloud support in an app stand as of May 2014? Answers seem to range from "with UIManagedDoc I only added a few lines of code" to many more involved examples that utilize more advanced notifications of changes to data both locally and in the cloud.

What I can't yet wrap my head around is where does one start, as of right now, when adding iCloud basic back-up across iCloud enabled devices (simulator included now for basic tests, apparently)? Say, if looking at code based on the Stanford Core Data lectures (see below), what are the steps needed to add iCloud syncing? What exactly has actually changed with iOS 7 to simplify this?

Do under-the-hood updates to iOS 7 really only require a "few lines" of code to get the code below ready for basic iCloud support?

I'm trying to get my head around what is the best practice, as of right now, to approach basic iCloud support in an app that used a UIManagedDocument. What changes to the code below, which already creates a doc in the Documents directory and uses that single document, need to be done?

Apple's own documentation hasn't been much help to me. I think I just need to see addition to code that I already understand to get me a point of reference.

From The Hegarty lectures, Stanford:

-(void)useDemoDocument
{
    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"Demo Document"];

    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];

    if (![[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
        //create
        [ document saveToURL:url
            forSaveOperation:UIDocumentSaveForCreating
           completionHandler:^(BOOL success) {
               if (success) {
                   self.context = document.managedObjectContext;
               }
           }];
    } else if (document.documentState == UIDocumentStateClosed){
        //open
        [document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                self.context = document.managedObjectContext;
                //[self refresh];
            }
        }];
    } else {
        //try to use it
        self.context = document.managedObjectContext;
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
vapul
  • 77
  • 8
  • 1
    It is is still complex and full of pitfalls. I have some sample applications and explanations for some of the options here http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ – Duncan Groenewald May 11 '14 at 04:39
  • I'd actually come across your site via google. I bookmarked it and have been looking through the samples, although most of it it is a bit beyond me. It's going to take me a while to determine how I can adapt that to the code above, because my knowledge level is basic. It is a great resource, and I can comment further if I manage to utilize the information. – vapul May 11 '14 at 06:39
  • My advice would be to stay away from UIManagedDocument but maybe someone else has had more success. It's not compatible with anything on OSX which is a problem for me. – Duncan Groenewald May 11 '14 at 10:43
  • so you would recommend getting the Core Data stack by initializing it in the app delegate? I had gone with the uimanageddoc on the advice of the stanford lectures specifically FOR future iCloud integration. I'm also looking into sync via Dropbox for now. – vapul May 11 '14 at 16:29
  • 1
    If you want to sync via Dropbox, integrating with Core Data+iCloud will not help. Take a look at my open source [Ensembles](http://ensembles.io) framework, which supports Core Data sync via iCloud and Dropbox, and is already shipping in apps that utilize UIManagedDocument. – Drew McCormack May 11 '14 at 18:03
  • You don't have to initialise Core Data in the app delegate if you want a Document based application. – Duncan Groenewald May 11 '14 at 21:05
  • Yes, I see that currently I have a Document based app therefore Core Data is not initialized in the app delegate (rather with the creation of the UIManagedDoc itself). At this point in time, are you saying that UIManagedDocument is too problematic so I should adopt another approach to Core Data (keeping eventual sync in mind as well)? Like I said, your site is a wealth of information, and I'm very interested in your opinion, especially as a newbie. – vapul May 11 '14 at 21:17

0 Answers0