Your workflow is unique 👨💻 - tell us how you use Dropbox here.
Forum Discussion
kacho
8 years agoExplorer | Level 3
Store upload request to a class var and cancel it later
Hi, I'm implementing Dropbox in my latest app. The app needs to do upload, share files and retrieve the shared links. This is done the following way. 1. The app checks if the file is previou...
Greg-DB
Dropbox Community Moderator
8 years agoWe'll be happy to help with this. Can you share the name and version of the SDK you're using, as well as the relevant code you have so far? Thanks in advance!
kacho
8 years agoExplorer | Level 3
Hi Greg and thanks for the quiq reply,
I'm away now and don't have access to the source but I believe it's the latest version of SwiftyDropbox.
As for the code - it's as I described:
First I get metadata for a given ID. If the ID is present, I list shared links and iterate to find what I'm looking for. If I find it the whole process completes as I olnly need the link (if the file is uploaded already).
If I can't find the link or the file doesn't have any shared links, I insert new share and from the response I get the link I need and all is done as in the previous case - it means the file is uploaded and I create a link and get it.
If, however, the file is not present in the system by the ID I initiate an upload either by URL (input) or by uploading chunks using SessionStart, SessionAppendV2 and SessionFinish.
After the whole upload is completed I, again, do a create share request to obtain a shred link and I'm done if no errors.
I can't post now any code but it's a common pattern I think (at least what I coul'd figure out from the SDK as I personally think it's quite confusing and the docs are really misleading but I don't want to criticise your work or to be rude).
As you can see all of those steps are diffrent requests and I want to be able to cancel eventually each and every one of them at some point - if the user decides not to upload or to dismiss sharing and so on. Every request is a differen type, from what I can see in the documentation.
The problem is I really can't understand if I can make a stored variable like, let's say:
var dropboxRequest: SomeDropboxType?
and when any of the requests start assign it to this var.
Later when the user wants to cancel just to call
dropboxRequest.cancel()
to stop proceeding the current task. I have, as I mentioned, my own way of tracking the following tasks regarding the same ID so stopping the current executing request will be enough I think.
My real problem is I don't get your logic behind the API, can't undestand you documentation so I just can't figure out such a simple think as declaring a variable to store the current request sent to Dropbox.
Thanks
- Greg-DB8 years ago
Dropbox Community Moderator
Thanks for elaborating! Right now you can do this explicitly, like:
var request: UploadRequest<Files.UploadSessionStartResultSerializer, VoidSerializer>?
self.request = DropboxClientsManager.authorizedClient!.files.uploadSessionStart(input: Data()).response(completionHandler: { (result, error) in if ((result) != nil) { print(result!) } else if (error != nil) { print(error!) } }) self.request!.cancel()You can get the exact return type for any method from the documentation, e.g., for uploadSessionStart. (Scroll to the right in "Declaration".)
Unfortunately, the SDK isn't set up in a such a way that would let you do this in a more general way, but I'll be sure to pass this along as a feature request.
- kacho8 years agoExplorer | Level 3
Hi again,
I tried this before I decided to post here and unfortunatelly it's not working. The compiler complains:
1. Cannot specialize non-generic type 'UploadRequest'
(and wants me to delete <Files.UploadSessionStartResultSerializer, VoidSerializer>) 2. 'UploadRequest' is ambiguous for type lookup in this context ! Found this candidate (Alamofire.UploadRequest) ! Found this candidate (SwiftyDropbox.UploadRequest)One other note - this way if I want to cancel a task as described earlier (get metadata, list shared links, create new if necessary or even upload before creating the link) I have to store each and every request in a separate var to be able to cancel all the task regarding the same object - file in this case. ANd the upload as I mentioned could be a regular
upload(path:mode:autorename:clientModified:mute:input:)
or the chunk model using
uploadSessionStart(_:input:) uploadSessionAppendV2(cursor:close:input:) uploadSessionFinish(cursor:commit:input:)
which all have different declaration types.
So for example if the user starts the whole process and, let's say the file has been uploaded earlier so the app starts a request to get the links. Imagine that the user uses a very poor connection and this process takes quite a time.
So I won't be able to cancel this unless I have another var to store this request. And the UI would look like unresponsive if I just wait for it to finish.
Another inconvenience is that, even if I store all the requests in separate vars, I need to figure out which one is currently active and their order will not be the same everytime.
And all of them are actually just HTTP requests that has one common method - cancel(), which purpose is to cancel the request. Couldn't they conform to a protocol which is the most common practice.
I know it's an open source project and I can go digg and change the code to make it work as I think is normal, but... You release an SDK which purpose is to serve people to integrate your service into their apps and facilitate them. I really don't think I should take a SDK and instead of finishing the job I'd start improving it.
Thanks again
- Greg-DB8 years ago
Dropbox Community Moderator
Yes, right now unfortunately there isn't a generalizable way to do this, e.g., with a protocol. You'd have to store each necessary type and keep track of them. I've filed this with the team as a feature request for a good way to do this though.
Anyway, the code I shared did work for me. Based on the error you're getting, it sounds like you're importing both SwiftyDropbox and Alamofire in the same file. (They both have their own "UploadRequest", so the declaration becomes ambiguous.)
You can fix that by either:
- removing your Alamofire import if you don't actually need it (or refactor it away)
- use "SwiftyDropbox.UploadRequest" instead, to make is unambigious
About Dropbox API Support and Feedback
Get help with the Dropbox API from fellow developers and experts.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!