0

I'm using Cloudinary to store images that the user uploads in my Swift 2 app. I have a question about the flow/architecture. Currently what I'm doing is that as soon as the user uploads the image, I send the image to Cloudinary, send the image id to my server, and immediately segue the user to the next screen. With the id, I can later reference the image on Cloudinary, so I technically don't need to wait for the response back from Cloudinary and keep the user waiting. Yes, if there is an upload failure, I won't know, but those are edge cases. This is the code I have:

@IBAction func btnRegister(sender: AnyObject) {
    let pictureId = self.btnImage.currentImage == NSUUID().UUIDString
    self.uploadToCloudinary(pictureId)

    //send to my server
    User.register(_userName, password: _password, picture: pictureId)
        .success { (value) in
         self.performSegueWithIdentifier("Home", sender: self)
         }
}

func uploadToCloudinary(pictureId:String){
        let image = UIImagePNGRepresentation(btnImage.currentImage!)! as NSData
        let uploader = CLUploader(_cloudinary, delegate: self)
        uploader.upload(image, options: ["public_id":pictureId], withCompletion:onCloudinaryCompletion, andProgress:onCloudinaryProgress)
}

func onCloudinaryCompletion(successResult:[NSObject : AnyObject]!, errorResult:String!, code:Int, idContext:AnyObject!) {
        //
}

However, I now realized that I need to save some additional details such as "version" that Cloudinary sends me back, so I might need to do something on onCloudinaryCompletion to send those details to my server. The thing is--that means I have to now make the user wait till the image finishes uploading before I can segue them to the next screen.

So my questions:

  1. In Swift, can I still do stuff in the completion handler even if the user has segeued away to another view controller? That way I can keep my current flow, and just save the successResult whenever it returns, but I don't have to keep my user waiting.

  2. Is there another recommended flow for doing something like this? Any suggestions welcome.

  3. For those of you who are familiar with Cloudinary--is there a way to know the version before uploading? Because all I really need is the version, and if I can know that before uploading, I can save that along with the picture Id, and forget about the onCloudinaryCompletion.

Prabhu
  • 12,995
  • 33
  • 127
  • 210

1 Answers1

1

From Cloudinary perspective you could use one of the following approaches:

Nadav Ofir
  • 778
  • 4
  • 6
  • For the first bulletpoint, I think I read somewhere that the version is required for images stored in folders? – Prabhu Nov 02 '16 at 20:24
  • It's the default behaviour of the SDKs to add `v1/` to all URLs containing folders structure. It can be omitted with no change in the results. The only impact will be in case of invalidations as explained here: http://cloudinary.com/documentation/upload_images#invalidating_cached_images_on_the_cdn – Nadav Ofir Nov 07 '16 at 10:51