简体   繁体   中英

Dropbox sync api

I am adding a observer to filesystem and Dbfile, but I am getting error that file already open. Please tell me where to close my file. Below is my code:

[filesystem addObserver:self forPathAndChildren:pathDB block:^(DBObserver complete){////observer to folder
                DBError *error= nil;
                NSLog(@"File(s) %@ changed!",pathDB);

                 DBFile *file=[filesystem openFile:pathDB error:&error];
                [file addObserver:self block:^() { ///observer to file


                    DBFileStatus *newerStatus = file.newerStatus;
                    if (newerStatus)
                    {
                        if (!newerStatus.cached)
                        {
                            NSLog(@"newerStatus.cached == NO; this means the file downloading");

                        }
                        else
                        {
                            // Update to the newly available version and print it out
                            [file update:nil];

                            NSData *fileData = [file readData:nil];
                            [fileData writeToFile:[[appDelegate.directoryPath stringByAppendingPathComponent:@"Projects"]  stringByAppendingFormat:@"/%@",path] atomically:YES];

                            [file close];

                    }

                }];

            }];

Please confirm, but my guess is that the error comes from this line:

DBFile *file=[filesystem openFile:pathDB error:&error];

That runs every time there's a change to pathDB , but you don't always close the file, so sometimes it will already be open.

I can't figure out what you're trying to do with your code, since you're setting up an observer on a path and then an observer on the file at that path... also, I'm pretty sure this code wouldn't compile, since you appear to be missing a closing curly brace.

One of the observers is probably redundant. Maybe all you want is this (no path observer at all)?

DBFile *file=[filesystem openFile:pathDB error:&error];
[file addObserver:self block:^() { ///observer to file
    DBFileStatus *newerStatus = file.newerStatus;
    if (newerStatus)
    {
        if (!newerStatus.cached)
        {
            NSLog(@"newerStatus.cached == NO; this means the file downloading");
        }
        else
        {
            // Update to the newly available version and print it out
            [file update:nil];

            NSData *fileData = [file readData:nil];
            [fileData writeToFile:[[appDelegate.directoryPath stringByAppendingPathComponent:@"Projects"]  stringByAppendingFormat:@"/%@",path] atomically:YES];
            [file close];
        }
    }
}];

The only reason I can think of to have both observers is if you want to handle the case where the file doesn't exist. In that case, the code you have might be close, but you'll need to not add an observer to the file if you already have one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM