You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

Robert S.138's avatar
Robert S.138
Helpful | Level 7
9 years ago

Creating a folder that already exists (iOS)

I went through this already for Android, and I learned that to create a folder that might already exists I only need to check the exception as follows:

catch (CreateFolderErrorException err) {
if( ! (err.errorValue.isPath() && err.errorValue.getPathValue().isConflict()) )
{ /* a real error, to be handled */ }
else  {/*this exception can safely be ignored*/}

Now I need to do the same thing in the Objective-C SDK for iOS.  In the completion block for createFolder I get:

^(DBFILESFolderMetadata *result, DBFILESCreateFolderError *cfError, DBError *error)

What is the equivalent check using "result", "cfError", and "error"?

 

  • In this case, you'd get a DBFILESCreateFolderError in your cfError variable, containing a "conflict" DBFILESWriteError in the path field.

    Here's an example of how you'd detect the different error cases, picking out the conflict error:

     [[client.filesRoutes createFolder:@"/testfolder"] response:^(DBFILESFolderMetadata *result, DBFILESCreateFolderError *cfError, DBError *error) {

    if (result) {
    NSLog(@"Create folder succeeded:");
    NSLog(@"%@", result);
    } else {
    NSLog(@"Create folder failed:");
    if (cfError) {
    NSLog(@"The error is a create folder error.");
    if ([cfError isPath]) {
    NSLog(@"The error is a path error.");
    if (cfError.path.isConflict) {
    NSLog(@"Couldn't create folder because there was something in the way.");
    } else {
    NSLog(@"Other create folder path error:");
    NSLog(@"%@", cfError.path);
    }
    }
    } else if (error) {
    NSLog(@"The error is a generic error.");
    NSLog(@"%@", error);
    }
    }

    }];

    (Apologies for the poor formatting.)

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    In this case, you'd get a DBFILESCreateFolderError in your cfError variable, containing a "conflict" DBFILESWriteError in the path field.

    Here's an example of how you'd detect the different error cases, picking out the conflict error:

     [[client.filesRoutes createFolder:@"/testfolder"] response:^(DBFILESFolderMetadata *result, DBFILESCreateFolderError *cfError, DBError *error) {

    if (result) {
    NSLog(@"Create folder succeeded:");
    NSLog(@"%@", result);
    } else {
    NSLog(@"Create folder failed:");
    if (cfError) {
    NSLog(@"The error is a create folder error.");
    if ([cfError isPath]) {
    NSLog(@"The error is a path error.");
    if (cfError.path.isConflict) {
    NSLog(@"Couldn't create folder because there was something in the way.");
    } else {
    NSLog(@"Other create folder path error:");
    NSLog(@"%@", cfError.path);
    }
    }
    } else if (error) {
    NSLog(@"The error is a generic error.");
    NSLog(@"%@", error);
    }
    }

    }];

    (Apologies for the poor formatting.)

     

  • Robert S.138's avatar
    Robert S.138
    Helpful | Level 7

    Thanks Gregory.  I had just figured out something like that from the comments in the Dropbox header files (more useful in this case than the HTML docs) and was just about to post something like that:

    if(cfError) {
    if((cfError.isPath) {
    DBFILESWriteError *werr = cfError.path;
    if(werr.isConflict) {
    NSLog(@"--This error can be safely ignored--");
    }
    }
    }

     

    But I wonder what would happen if there is a "conflict", not because the folder already exists in Dropbox, but because a FILE by that name already exists in Dropbox.  Obviously such an error should not be ignored.  But will such an error distinguish itself from the folder already existing condition?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    That would also be a path conflict error. The conflict error itself just means that something is already at the path. You can then check that DBFILESWriteConflictError to see if it was a file or folder, e.g. to expand on my last example:

     [[client.filesRoutes createFolder:@"/testfolder"] response:^(DBFILESFolderMetadata *result, DBFILESCreateFolderError *cfError, DBError *error) {

    if (result) {
    NSLog(@"Create folder succeeded:");
    NSLog(@"%@", result);
    } else {
    NSLog(@"Create folder failed:");
    if (cfError) {
    NSLog(@"The error is a create folder error.");
    if ([cfError isPath]) {
    NSLog(@"The error is a path error.");
    if (cfError.path.isConflict) {
    NSLog(@"Couldn't create folder because there was something in the way.");
    if (cfError.path.conflict.isFolder) {
    NSLog(@"Couldn't create folder because a folder already exists there.");
    } else if (cfError.path.conflict.isFile) {
    NSLog(@"Couldn't create folder because a file already exists there.");
    } else {
    NSLog(@"Other create folder path conflict error:");
    NSLog(@"%@", cfError.path.conflict);
    }
    } else {
    NSLog(@"Other create folder path error:");
    NSLog(@"%@", cfError.path);
    }
    }
    } else if (error) {
    NSLog(@"The error is a generic error.");
    NSLog(@"%@", error);
    }
    }

    }];

     

     

  • Robert S.138's avatar
    Robert S.138
    Helpful | Level 7

    At first I got an error with (cfError.path.conflict.isFolder), but then I realized that I just needed to include:

    #import <DBFILESWriteConflictError.h>

    and now it compiles without errors.  Thanks.

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    Glad to hear that's working now. That import shouldn't really be necessary though. What version of the SDK do you have installed? What other imports do you have?

  • Robert S.138's avatar
    Robert S.138
    Helpful | Level 7

    Here are my Dropbox imports:

     

    #include <DBAUTHAuthError.h>

    #include <DBFILESWriteConflictError.h>

    #include <DBErrors.h>

     

    If I comment out the middle one, I get the error on 'isFolder".

    I suggest that you put the required import files into the documentation, like Microsoft does with all their Win32 API functions.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    I see, thanks for clarifying. We do have a general import you can use as shown in the readme:

    #import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
  • Robert S.138's avatar
    Robert S.138
    Helpful | Level 7

    When I try to add that import to my source file, I get a file not found error.  I installed Dropbox with Cocoapods.  Is there some different path I should use with that?  Or is there some build setting I am lacking?  I poked around with a Finder window and did not find any such header file, although I did locate all the other Dropbox header files.

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    This did change a bit over the versions.  Are you using the latest version of the SDK? If not, please pod update and try again. 

  • Robert S.138's avatar
    Robert S.138
    Helpful | Level 7

    That fixed it.  Now just one import replaces all the other Dropbox imports I was using.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,889 PostsLatest Activity: 2 hours ago
327 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!