0

The two conversion methods below for mapping between the PHPhoto localIdentifier to the corresponding cloudIdentifier work but it feels too heavy. Do you have suggestions on how to rewrite to a more elegant (easier to read) form?

The sample code in the Apple documentation found in PHCloudIdentifier https://developer.apple.com/documentation/photokit/phcloudidentifier/ does not compile in xCode 13.2.1.

It was difficult to rewrite the sample code because I made the mistake of interpreting the Result type as a tuple. Result type is really an enum.

 func localId2CloudId(localIdentifiers: [String]) -> [String] {
    var mappedIdentifiers = [String]()
   let library = PHPhotoLibrary.shared()
    let iCloudIDs = library.cloudIdentifierMappings(forLocalIdentifiers: localIdentifiers)
    for aCloudID in iCloudIDs {
        //'Dictionary<String, Result<PHCloudIdentifier, Error>>.Element' (aka '(key: String, value: Result<PHCloudIdentifier, Error>)')
        let cloudResult: Result = aCloudID.value
        // Result is an enum .. not a tuple
        switch cloudResult {
            case .success(let success):
                let newValue = success.stringValue
                mappedIdentifiers.append(newValue)
            case .failure(let failure):
                // do error notify to user
                let iCloudError = savePhotoError.otherSaveError // need to notify user

        }
    }
    return mappedIdentifiers
}

func cloudId2LocalId(assetCloudIdentifiers: [PHCloudIdentifier]) -> [String] {
        // patterned error handling per documentation
    var localIDs = [String]()
    let localIdentifiers: [PHCloudIdentifier: Result<String, Error>]
       = PHPhotoLibrary
            .shared()
            .localIdentifierMappings(
              for: assetCloudIdentifiers)

    for cloudIdentifier in assetCloudIdentifiers {
        guard let identifierMapping = localIdentifiers[cloudIdentifier] else {
            print("Failed to find a mapping for \(cloudIdentifier).")
            continue
        }
        switch identifierMapping {
            case .success(let success):
                localIDs.append(success)
            case .failure(let failure) :
                let thisError = failure as? PHPhotosError
                switch thisError?.code {
                    case .identifierNotFound:
                        // Skip the missing or deleted assets.
                        print("Failed to find the local identifier for \(cloudIdentifier). \(String(describing: thisError?.localizedDescription)))")
                    case .multipleIdentifiersFound:
                        // Prompt the user to resolve the cloud identifier that matched multiple assets.


                    default:
                        print("Encountered an unexpected error looking up the local identifier for \(cloudIdentifier). \(String(describing: thisError?.localizedDescription))")
                }
          }
        }
    return localIDs
}
  • You could convert to async/await... – matt Feb 23 '22 at 18:49
  • Not sure how async/wait would simplify the code. Can you show an example of how that would work?. – Will Loew-Blosser Feb 24 '22 at 19:24
  • Your talk of heavy, elegant, easier, and simplify is just so much opinionated fluff. So no, I can't show that. My suggestion is just a suggestion. If you are serious, try it yourself and see if you like it better, since the whole question is about what you like. – matt Feb 24 '22 at 19:37
  • You are right Matt, I do have an opinion that code needs to be clear so that I or another developer don't waste time trying to puzzle out the logic. I'll raise the issue in the developer forum because the enum 'Result' hides the actual mapping value returned. This was posted because the Apple sample code does not compile and to save others the little headache of puzzling out the obscure compile problems. But I'm not happy with this reworked solution and hope others have a better way. – Will Loew-Blosser Feb 24 '22 at 19:55
  • You see, I don't find the code you posted puzzling. It's not necessarily how I would _do_ it, but I don't find it difficult to read. But perhaps that's because I know what a Result is? – matt Feb 24 '22 at 20:21

0 Answers0