Question

My app had a different system to keep certain data in sync before. I would keep track of the data in a simple file, then keep it in sync with Dropbox with a code similar to this:

DBPath *newPath = [[DBPath root] childPath:[NSString stringWithFormat:@"metadata/%@", tagsFileName]];

NSString *metadata = [(FTIBAppDelegate *)[[UIApplication sharedApplication] delegate] getMetadataPath];
NSString *collectionsFile = [NSString stringWithFormat:@"%@/%@", metadata, tagsFileName, nil];

DBFilesystem *theSystem = [DBFilesystem sharedFilesystem];
if([theSystem completedFirstSync])
{
    NSError *erri = nil;
    if(!file.open)
    {
        file = [[DBFilesystem sharedFilesystem] openFile:newPath error:&erri];
    }

    __block DBFileStatus *newerStatus = file.newerStatus;
    if(newerStatus != nil)
    {
        [file update:nil];
    }
    __block DBFileStatus *status = file.status;

    //__block NSString *contents = [file readString:nil];

    if(erri == nil)
    {
        [file addObserver:self block:^{
            newerStatus = file.newerStatus;
            status = file.status;
            if(status.cached)
            {
                NSString *contents = [file readString:nil];
                [contents writeToFile:collectionsFile atomically:NO encoding:NSUTF8StringEncoding error:nil];
            }

            if(newerStatus != nil)
            {
                NSString *contents = [file readString:nil];
                [file update:nil];
                [contents writeToFile:collectionsFile atomically:NO encoding:NSUTF8StringEncoding error:nil];
            }
        }];
    }
}

Observing an individual file was very easy. Like you can see in the code above, I'd observe the individual file, then when it got updated, I'd read the contents of the file and write them locally.

Now I'm trying to observe a path that contains subdirectories. The subdirectories and their contents can be variable, so I cannot observe each individual directory and file manually.

I was looking at the Dropbox documentation, and I found this method:

- (BOOL)addObserver:(id)observer forPathAndDescendants:(DBPath *)path block:(DBObserver)block

Which says:

Add an observer to be notified any time the folder at path changes or a file or folder contained somewhere beneath path changes.

Fair enough. Reading the description it looks like it is exactly what I need to observe everything under a certain path.

But here's the problem: It doesn't seem to notify me what file or folder has changed, so I cannot figure out the file that has changed in Dropbox so I cannot overwrite my local copy.

So far I got this:

DBPath *newPath = [[DBPath root] childPath:@"metadata/__MIGNORICOLLECTIONS__"];
NSString *metadata = [(FTIBAppDelegate *)[[UIApplication sharedApplication] delegate] getMetadataPath];
NSString *localPath = [NSString stringWithFormat:@"%@/%@", metadata, @"__MIGNORICOLLECTIONS__"];

DBFilesystem *theSystem = [DBFilesystem sharedFilesystem];
if([theSystem completedFirstSync])
{
    [theSystem addObserver:self forPathAndDescendants:newPath block:^{
    }];
}

But I really don't know where to go from there. If the block doesn't give me the file that has changed/is being changed, there is nothing I can do to overwrite my local files and effectively keep both Dropbox and my local contents in sync.

Was it helpful?

Solution

If you need to know what files changed via the Sync API, your only choice is to compare before-and-after when something changes. So you'll need to maintain information about your local state, like what rev of each file exists on the local device and then compare that with the new state every time it changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top