0

I've got an OS X app syncing a single document through a ubiquity container back and forth to an iOS equivalent app. The iOS app receives data whenever it changes on the Mac side and sends it whenever it changes on the iOS side (so the iOS app is working all around), and the Mac app sends the data whenever it is changed on the Mac side and it receives the data when the app is launched, but it doesn't seem to be checking again for any data while it runs. I'd like it to update with any changes automatically and immediately, like the OS X "Notes" app does from changes on the iOS side.

At launch, this is the relevant function that gets called:

+(NSMutableDictionary *)getAllNotes {
    if(allNotes == nil) {
        allNotes = [[NSMutableDictionary alloc]initWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:kAllNotes]];

        cloudDoc = [[CloudDocument alloc]initWithContentsOfURL:[self notesURL] ofType:NSPlainTextDocumentType error:nil];
        [cloudDoc saveToURL:[self notesURL] ofType:NSPlainTextDocumentType forSaveOperation:NSSaveOperation error:nil];
    }
    return allNotes;
}

and that "CloudDocument" class (which is a subclass of NSDocument) includes:

#import "Data.h"

@implementation CloudDocument

-(NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    return [NSKeyedArchiver archivedDataWithRootObject:[Data getAllNotes]];
}

-(BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
    NSDictionary *dict = (NSDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)data];
    [Data didReceiveCloudData:dict];
    return YES;
}

+(BOOL)autosavesInPlace {
    return YES;
}

@end

which kicks it back to:

+(void)didReceiveCloudData:(NSDictionary *)d {
    allNotes = [[NSMutableDictionary alloc]initWithDictionary:d];
    [[NSUserDefaults standardUserDefaults] setObject:allNotes forKey:kAllNotes];

    [cloudDoc updateChangeCount:NSChangeDone];
}

I think the problem is just that I don't have any part of my code that is equivalent to the phrase "check periodically to see if the ubiquity container has changed, and then do..." etc. I'm sure there's a well-known process for this (some notification event in NSDocument or something), but I've searched around and everything I find is either for iOS/UIDocuments instead of OS X/NSDocuments, or it's all theory and over my head without any tangible code samples to comb through and pick apart.

Can anyone help me out with a method for registering that an iCloud document in the ubiquity container has changed, and ideally where to put it (AppDelegate, CloudDocument.m, etc)? I only have one file syncing around, signified by the constant kAllNotes, so I don't need to track a bunch of different files or anything. I'm pretty sure I can use the code that runs at launch to do what needs to be done, I just can't figure out what to do to start the auto-syncing process.

Thank you in advance!

PS I'm still a beginner, so tutorials and code samples are much appreciated.

Nerrolken
  • 1,975
  • 3
  • 24
  • 53

2 Answers2

3

You're looking for NSMetadataQuery - it does a spotlight-like, continuously running search for any type of file - and is available on both iOS and OS X (indeed on iOS it can only be used for observing changes to the ubiquity container). I don't have any one link on how to use this - the Apple docs are too general to make much sense initially but do a search on 'NSMetadataQuery iCloud' and you should be sorted - there is tons of information out there on this topic.

With NSMetadataQuery you receive a notification every time something in the observed folder system changes, and it's not just applicable to UIDocument files even if a lot of examples are worried about UIDocuments.

Rob Glassey
  • 2,237
  • 20
  • 21
  • Thanks! Can I just ask for clarification: would I set up that NSMetadataQuery in the initialization method of the CloudDocument object (subclassed from NSDocument)? Or do I do it in applicationDidFinishLaunching, or what? I'm new to NSMetadataQuery, Notifications and all of this stuff, so any advice you can offer in setting it all up is very much appreciated. – Nerrolken Jun 16 '13 at 02:00
  • 1
    Pretty much wherever you want. On my apps I create it after I've determined the ubiquity URL with URLForUbiquityContainerIdentifier: and only if the user has iCloud switched on. This makes sense, as there is no point setting up the query if you haven't got a valid ubiquity URL. There are a million different ways to approach this, just choose the way feels more natural for your app. I tend to have a 'CloudController' object that observes what is going on in the ubiquity container and reacts to things like user logout, cloud availability etc, and it's in this that I put my metadata queries. – Rob Glassey Jun 16 '13 at 09:31
  • Thanks! If you have the time (and the inclination), could you weigh in on [another question I posted](http://stackoverflow.com/questions/17129946/setting-up-nsmetadataquerydidupdatenotification-for-a-simple-response) about this topic? Another user has been helping me out, but a comment thread has developed and I'm trying to track down exactly what the remaining problem is. As I understand it I've got all my code set up correctly aside from not knowing a file extension, but there might be another problem as well. I'm SO close to finishing this app I can taste it. Thank you again for your help! – Nerrolken Jun 16 '13 at 09:52
2

Indeed, NSMetadataQuery it is - it feels like a hack but appears to be the only way to monitor changes to standard (non-UIDocument) files.

Here's a sample project I've compiled. It lets you upload an image from the camera roll, then monitors changes to the ubiquitous Documents folder:

https://github.com/versluis/iCloud-Images

Jay Versluis
  • 2,062
  • 1
  • 19
  • 18