How can I stop a stream when using STTWitter

2019-08-12 06:24发布

问题:

On iOS (but I think this would be just the same on OSX) I'm using STTWitter.

Among other things, I want to use it to stream the main public timeline.

I've successfully used getStatusesSampleDelimited:stallWarnings:progressBlock:stallWarningBlock:errorBlock: to start streaming tweets.

How can I stop the stream once I've got enough, or want to switch to a different stream (e.g. streaming a search)?

I've tried destroying the STTWitterAPI object - with no effect. I can't see a method on the object to stop streaming, and I've traced the source code through and don't see any way I can stop a stream once it's started.

What have I missed?

回答1:

The library didn't support cancelling request, so I just added this feature:

id request = [twitter getStatusesSampleDelimited:nil
                                   stallWarnings:nil
                                   progressBlock:^(id response) {
    // ...
} stallWarningBlock:nil
         errorBlock:^(NSError *error) {
    // ...
}];

// ...

[request cancel]; // when you're done with it

After request cancellation, the error block is called once with a cancellation error.

Let me know if it doesn't fulfil your needs.