Re: kvo
Re: kvo
- Subject: Re: kvo
- From: Ken Tozier <email@hidden>
- Date: Tue, 14 Jun 2011 08:08:23 -0400
Have you tried NSInvocationOperations? They allow you to queue up your own handler and you could do any required checking in that
Something like
// init requestQueue in your class's init method
// then whenever you need to queue up a url load
// do something like this
- (void) queueRequest:(NSURL *) inRequest
{
NSInvocationOperation *operation = [[[NSInvocationOperation alloc]
initWithTarget: self
selector: @selector(performRequest:)
object: inRequest]
autorelease];
[operation addObserver: self
forKeyPath: @"isFinished"
options: NSKeyValueObservingOptionNew
context: NULL];
[operation addObserver: self
forKeyPath: @"isCancelled"
options: NSKeyValueObservingOptionNew // not sure if this it the right option here, You'd have to check
context: NULL];
[requestQueue addOperation: operation];
}
- (void) performRequest:(NSURL *) inRequest
{
// do your thing with the url, including checking for validity
// in this method
}
- (void)observeValueForKeyPath:(NSString *) inKeyPath
ofObject:(id) inObject
change:(NSDictionary *) inChange
context:(void *) inContext
{
[inObject removeObserver: self forKeyPath: @"isFinished"];
[inObject removeObserver: self forKeyPath: @"isCancelled"];
if ([inKeyPath isEqualToString: @"isFinished"])
{
// do your thing with successes
}
else if ([inKeyPath isEqualToString: @"isCancelled"])
{
// do your thing with failures
}
}
On Jun 13, 2011, at 8:52 PM, Ariel Feinerman wrote:
> Hi,
>
> how to cancel operation which is performing?
>
> I think it is not safe:
>
> - (void) beginOperation {
>
> [_operation cancel];
>
> _operation = [[Operation alloc] initWithURL: [NSURL URLWithString: @"
> http://rss.cnn.com/rss/cnn_topstories.xml"]];
>
> _queue = [NSOperationQueue new];
>
> [_operation addObserver: self forKeyPath: @"isFinished" options:
> NSKeyValueObservingOptionNew context: NULL];
>
> [_operation addObserver: self forKeyPath: @"isCancelled" options:
> NSKeyValueObservingOptionNew context: NULL];
>
> [_queue addOperation: _operation];
>
> }
>
>
> - (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object
>
> change: (NSDictionary *) change context: (void *) context {
>
> if ([keyPath isEqualToString: @"isFinished"]) {
>
> if ([[change valueForKey: NSKeyValueChangeNewKey] boolValue]) {
>
> [rootViewController performSelectorOnMainThread: @selector(setItemList:)
> withObject: [_operation itemList] waitUntilDone: NO];
>
> static BOOL firstTime = YES;
>
> if (firstTime) {
>
> [self performSelectorOnMainThread: @selector(setRootView:) withObject: nil
> waitUntilDone: NO];
>
> firstTime = NO;
>
> }
>
> }
>
> }
>
> else if ([keyPath isEqualToString: @"isCancelled"]) {
>
> if ([[change valueForKey: NSKeyValueChangeNewKey] boolValue]) {
>
> if ([_operation error]) {
>
> [self performSelectorOnMainThread: @selector(errorOccurs:) withObject:
> [_operation error] waitUntilDone: NO];
>
> }
>
> }
>
> }
>
> [_operation removeObserver: self forKeyPath: @"isFinished"];
>
> [_operation removeObserver: self forKeyPath: @"isCancelled"];
>
> [_operation release];
>
> _operation = nil;
>
> [_queue release];
>
> _queue = nil;
>
> }
>
> --
> best regards
> Ariel
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >kvo (From: Ariel Feinerman <email@hidden>) |