Archived Forum Post

Index of archived forum posts

Question:

How to cancel S3 download (iOS)

Apr 03 '13 at 11:15

I'm starting download like this: BOOL success = [http S3_DownloadFile: bucketName objectName: objectName localFilePath:filenameWithTempExtension];

It works fine but if I need to cancel download, how do I do it?


Answer

For now, you can call Http.CloseAllConnections prior to calling S3_DownloadFile. This will ensure that a new connection is established for the download. What is currently happening is that HTTP object is trying to use the pre-existing connection because the previous HTTP response did not indicate that the connection should be closed. A TCP socket that becomes disconnected by the remote peer closing its end is not noticed until an attempt to read is tried. This is what happens, and the method fails. I will work on adding an internal automatic re-try to re-establish the connection so that it doesn't need to be worried about. This change will be in v9.4.1. For now though, you can explicitly make sure connections are closed by calling Http.CloseAllConnections.


Answer

To cancel an HTTP method call, setup a callback object by deriving from CkHttpProgress:

class MyHttpProgress : public CkHttpProgress { private: bool m_timeToAbort;

public:
MyHttpProgress(void) : m_timeToAbort(false) { }
virtual ~MyHttpProgress(void) { }

void PercentDone(int pctDone, bool *abort) 
    { 
    printf("PercentDone: %d percent\n",(int)pctDone);
    *abort = m_timeToAbort;
    }
void AbortCheck(bool *abort) 
    { 
    printf("----------- AbortCheck -----------\n");
    *abort = m_timeToAbort;
    }

void HttpBeginReceive(void) { printf("HttpBeginReceive!\n"); }
void HttpEndReceive(bool success) { printf("HttpEndReceive!\n"); }
void HttpBeginSend(void) { printf("HttpBeginSend!\n"); }
void HttpEndSend(bool success) { printf("HttpEndSend!\n"); }

void ReceiveRate(unsigned long byteCount, unsigned long bytesPerSec) 
    { 
    printf("ReceiveRate: byteCount = %d, bytesPerSec = %d\n",
    (int)byteCount,(int)bytesPerSec);
    }
void ProgressInfo(const char *name, const char *value)
    {
    printf("%s, %s\n",name,value);
    }

};

Then set the callback like this:

    CkHttp http;

MyHttpProgress myProgress;
http.put_EventCallbackObject(&myProgress);

Methods can be aborted from the PercentDone or AbortCheck callbacks. (PercentDone events are only called when it's possible to know the percentage completion. In some cases, such as for some method calls, or for chunked HTTP responses, it is impossible to know the percent complete.)

AbortCheck events fire at a frequency set by the HeartbeatMs property setting (which defaults to 0 for no AbortCheck events). PercentDone events, because they allow for aborting, count as an AbortCheck.


Answer

PS> If the HTTP method was backgrounded by setting the UseBgThread property, then your foreground thread may abort the method running in the background thread at any time by calling CkHttp::BgTaskAbort.


Answer

Also, if using Objective-C, then the concept of using the event callback class is the same, and the methods are the same, except the class is CkoHttpProgress. See:
http://www.chilkatforum.com/questions/292/event-callbacks-in-ios-objective-c


Answer

Thank you, these answers really solved my issues.