Search code examples
iosbox-apiboxapiv2

Box IOS SDK V2 cancel download/upload operation


I'm using the objective-c box SDK to manage files using the dedicated method

[[BoxSDK sharedSDK].filesManager downloadFileWithID:fileID
                                       outputStream:outputStream
                                     requestBuilder:nil
                                            success:successBlock
                                            failure:failureBlock
                                           progress:progressBlock];

I need to be able to cancel the download task but I'm not able to find a way to do that ! I need also to be able to cancel upload tasks, but I guess the way of doing that will be the same ...

Do anyone manage to achieve that ?


Solution

  • filesManager returns BoxAPIDataOperation. Ultimately BoxAPIDataOperation inherits from NSOperation and Box base class for it is BoxAPIOperation. To cancel BoxAPIOperation you just send it a message cancel.

    In fact all of resource managers in BoxSDK return classes that inherit from BoxAPIOperation. You can find (void)cancel in BoxAPIOperation.m.

    So in your case you'd want something like this

    // property to store pointer to currently active download operation.
    // it is weak, because you don't want to retain it. after download is completed, cancelled or failed
    @property (nonatomic, readwrite, weak) BoxAPIDataOperation *downloadOperation;
    ....
    
    self.downloadOperation = [[BoxSDK sharedSDK].filesManager downloadFileWithID:fileID
                                                                outputStream:outputStream
                                                              requestBuilder:nil
                                                                     success:successBlock
                                                                     failure:failureBlock
                                                                    progress:progressBlock];
    ...
    - (void)dealloc
    {
       [self.downloadOperation cancel];
    }
    // only if want to keep self in object and want to just cancel operation
    - (void)userPressedCancelButton:(id)sender
    {
       [self.downloadOperation cancel];
    }