Will Loew-Blosser
Will Loew-Blosser

Reputation: 278

PHPhoto localIdentifier to cloudIdentifier conversion code improvements?

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
}

Upvotes: 1

Views: 374

Answers (0)

Related Questions