title: How do I use the Dropbox Sync API to keep a directory and its contents in sync?
body: <p>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:</p>
  
  <pre><code>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:&amp;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];
              }
          }];
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I was looking at the Dropbox documentation, and I found this method:</p>
  
  <pre><code>- (BOOL)addObserver:(id)observer forPathAndDescendants:(DBPath *)path block:(DBObserver)block
  </code></pre>
  
  <p>Which says:</p>
  
  <blockquote>
    <p>Add an observer to be notified any time the folder at path changes or
    a file or folder contained somewhere beneath path changes.</p>
  </blockquote>
  
  <p>Fair enough. Reading the description it looks like it is <em>exactly</em> what I need to observe everything under a certain path.</p>
  
  <p>But here's the problem: It doesn't seem to notify me <em>what</em> file or folder has changed, so I cannot figure out the file that has changed in Dropbox so I cannot overwrite my local copy.</p>
  
  <p>So far I got this:</p>
  
  <pre><code>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:^{
      }];
  }
  </code></pre>
  
  <p>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.</p>