About errors, when use moveItemAtPath

Do any errors occur, when use "moveItemAtPath" on processing?

If so, why or in what case does the error occur?

When any error occurs, what is the effect on moving files and folders?

Do the moving files move to the destination and the rest of files remain in the original location?

Or something else will happen to them?


Development environment : Xcode10.0

Device : iPad / iPhone of over iOS8.0

Replies

Do you get an error or not ?

If yes, please describe the error, conditions of occurence…


moveItemAtPath can throw errors. So, yes, it can fail and you should catch and proceed errors.

Type of error: you provided invalid path.

Or you do not have authorization to move here

Or no more room on destination…


In Finder, when you copy or move a folder, alreaady copied files remain copied.


Note: you have posted in the wrong part of the forum ; this is not an Interface Builder topic.

The behavior likely depends on what file system is in use. If both source and destination are on the same volume, and it's a HFS+ volume, the move is likely an atomic operation, which means it completely succeeds or it completely fails — no partial moves.


If they're on different volumes, the documentation says there will be a copy followed by a remove. That's not atomic, so it something goes wrong partway through, files at the destination may be in an incomplete state.


Moves involving network volumes may have yet different behavior.



Unfortunately, there's no single guarantee of behavior that applies to all scenarios.

Thank you for the replay.

Although no error actually occurs, I do not want to cause errors at all,

so I'm searching in advance whether there is anything I should be careful about.

This question is about the processing of iOS app.

As processing, I want to move files and folders inside the app to another folder in the same app.

Therefore, I think if I specify a valid path, that processing normally ends. Is that right?

Yes, if the path is valid, you should not have error.


Nevertheless, you should catch any potential error:


do {
  try moveItemAtPath()
     // Continue what you need to do if successful
} catch {
     // Better to report error message here
     return // If you are inside a func
}

Thank you for the answers.

I know this is a difficult question to answer but if you know what any potential errors are, could you advise me about that please?

Because this is for the customer’s application, I would like to eliminate the cause of the errors at all.

I appreciate for your cooperation.

Search for FoundationErrors.h on your Mac, you will get an extensive list.

I think here is the most extensive list (some may not be related to moveItemAtPath) :

NS_ERROR_ENUM(NSCocoaErrorDomain) {

// File system and file I/O related errors, with NSFilePathErrorKey or NSURLErrorKey containing path or URL

NSFileNoSuchFileError = 4, // Attempt to do a file system operation on a non-existent file

NSFileLockingError = 255, // Couldn't get a lock on file

NSFileReadUnknownError = 256, // Read error (reason unknown)

NSFileReadNoPermissionError = 257, // Read error (permission problem)

NSFileReadInvalidFileNameError = 258, // Read error (invalid file name)

NSFileReadCorruptFileError = 259, // Read error (file corrupt, bad format, etc)

NSFileReadNoSuchFileError = 260, // Read error (no such file)

NSFileReadInapplicableStringEncodingError = 261, // Read error (string encoding not applicable) also NSStringEncodingErrorKey

NSFileReadUnsupportedSchemeError = 262, // Read error (unsupported URL scheme)

NSFileReadTooLargeError API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 263, // Read error (file too large)

NSFileReadUnknownStringEncodingError API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) = 264, // Read error (string encoding of file contents could not be determined)

NSFileWriteUnknownError = 512, // Write error (reason unknown)

NSFileWriteNoPermissionError = 513, // Write error (permission problem)

NSFileWriteInvalidFileNameError = 514, // Write error (invalid file name)

NSFileWriteFileExistsError API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = 516, // Write error (file exists)

NSFileWriteInapplicableStringEncodingError = 517, // Write error (string encoding not applicable) also NSStringEncodingErrorKey

NSFileWriteUnsupportedSchemeError = 518, // Write error (unsupported URL scheme)

NSFileWriteOutOfSpaceError = 640, // Write error (out of disk space)

NSFileWriteVolumeReadOnlyError API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 642, // Write error (readonly volume)


Read also

https://stackoverflow.com/questions/31977738/how-to-find-the-kind-of-errors-a-method-may-throw-and-catch-them-in-swift


To report the error:


do {
  try moveItemAtPath()
     // Continue what you need to do if successful
}  catch let error as NSError {
     // Better to report error message here
    print("Something went wrong: \(error)")
    return // If you are inside a func
}