1

I have an array of appointments and I'm trying to grab all of the photos for these appointments from our windows azure blob storage. First, I want to get the list of blobs with the associated appointmentId so I can download and store them properly afterwards.

I'm using PromiseKit but I'm not at all sure about how to use PromiseKit in a loop:

for appointment in appointments {
  // Get blobs
}

Here's my code so far. Any help is greatly appreciated!

func getBlobsPromise(appointmentId: Int32) -> Promise<[BlobDownload]>  {
return Promise { seal in
    var error: NSError?
    var blobDownloads = [BlobDownload]()
    container = AZSCloudBlobContainer(url: URL(string: containerURL)!, error: &error)
    if ((error) != nil) {
        print("Error in creating blob container object. Error code = %ld, error domain = %@, error userinfo = %@", error!.code, error!.domain, error!.userInfo)
        seal.reject(error!)
    }

    let prefix: String = "AppointmentFiles/\(appointmentId)"

    container?.listBlobsSegmented(with: nil, prefix: prefix, useFlatBlobListing: true, blobListingDetails: AZSBlobListingDetails(), maxResults: 150) { (error : Error?, results : AZSBlobResultSegment?) -> Void in

        if error != nil {
            seal.reject(error!)
        }

        for blob in results!.blobs!
        {
            let blobInfo = blob as! AZSCloudBlob
            if blobInfo.blobName.lowercased().contains("jpg") || blobInfo.blobName.lowercased().contains("jpeg") {
                let blobDownload: BlobDownload = BlobDownload(appointmentId: Int(jobId), blob: blobInfo)
                blobDownloads.append(blobDownload)
            }

        }

        seal.fulfill(blobDownloads)
    }
}

}

That returns the blobs as expected but I want to get all of the blobs for all of the appointments before proceeding. Here's what I tried (among other things):

func getBlobsForAllJobs(appointmentIds: [Int32]) -> Promise<[BlobDownload]> {
return Promise { seal in
    let count = appointmentIds.count - 1
    let promises = (0..<count).map { index -> Promise<[BlobDownload]> in
        return getBlobsPromise(agencyCode: agencyCode, appointmentId: appointmentIds[index])
    }
    when(fulfilled: promises).then({ blobDownloads in
        seal.fulfill(blobDownloads)
    })
}

}

EDIT 1

I solved this using a DispatchGroup and completion handler. Here's the code in case someone is interested. If there are alternate (better) ways of doing this I'd love to hear them. I'm a c# guy just getting into Swift.

func getBlobsToDownload(appointmentIds: [Int32], completion: @escaping ([BlobDownload]) -> Void) {
var myBlobsToDownload = [BlobDownload]()
let myGroup = DispatchGroup()

for apptId in appointmentIds {
    myGroup.enter()

    getBlobs(appointmentId: apptId) { (blobDownloads) in
        print("Finished request \(apptId)")
        print("Blobs fetched from apptId \(apptId)  is \(blobDownloads.count)")
        for blobDownload in blobDownloads {
            myBlobsToDownload.append(blobDownload)
        }
        myGroup.leave()
    }
}

myGroup.notify(queue: .main) {
    print("Finished all requests.")
    completion(myBlobsToDownload)
}

}

Energy CJ
  • 125
  • 2
  • 11

0 Answers0