Domanda

I have 2 buttons in my View. fetch and stop buttons. And when i press fetch button my method get data and put it to textView's text. But if i press stop button i want to stop fetching. I know that i can't stop request but when i press stop button it mustn't change textView's text. To be more clear when it finished fetching data, my program mustn't get back.
Here's my fetch method:

- (void) pressedFetch
{
    dispatch_async(dispatch_get_main_queue(), ^{
        textView.text = [NSString stringWithFormat:@"%@", [self fetchJson]];
    });
}  

i want to do it by like callBack method. How can i do that?

È stato utile?

Soluzione 4

Actually it was wrong way. And i changed my method. I did it with NSURLSession. it has method that is named invalidateAndCancel. if i use this method my session is canceled and fetching is stoped.

- (void) pressedFetch
{
  NSString *jsonUrl = @"http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=demo";
NSURL *url = [NSURL URLWithString:str];
NSURLSessionConfiguration *conf = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    session = [NSURLSession
               sessionWithConfiguration:conf
               delegate:self
               delegateQueue:[NSOperationQueue mainQueue]];
    task = [session downloadTaskWithRequest:request]; //NSURLSessionDownloadTask *task = ...
    [task resume];
}

- (void) pressedStop
{
  [session invalidateAndCancel]; //NSURLSession *session = ...
}  

it was so easy )))

Altri suggerimenti

Don't use GCD, use the higher level NSOperation API which offers cancellation support. In your operation subclass you can then check for the operation being cancelled before using the downloaded data. To cancel you use the operation queue cancelAllOperations method.

Your operation can be an instance of NSBlockOperation, and assuming that fetchJson is synchronous, something like:

NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{
    id json = [self fetchJson];

    if (![self isCancelled]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            textView.text = [NSString stringWithFormat:@"%@", json];
        });
    }
}];

If you just don't want to show the fetched data on tapping Stop button,

- (IBAction)stop:(id)sender
{
     textView.text = @""
}

or if you are using NSURLConnection to fetch the data from server, you can call Cancel on it

- (IBAction)stop:(id)sender
{
    [urlConnection cancel];  // NSURLConnection *connection;
    /* After this method is called, the connection’s delegate no longer receives any messages for the connection. If you want to reattempt the connection, you should create a new connection object.*/
}

First of all you have to check you data already download in your class or not. if not than you can download data and store in a file through archive in document directory next time check it is available in document directory and the unarchived your data and use it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top