我正在尝试使用从另一个NSOperation
开始的NSOperation这是最内在的NSOperation:
#pragma mark - OVERRIDE
- (void)main
{
@autoreleasepool {
if (self.isCancelled) {
return;
}
NSURL *url = [NSURL URLWithString:self.urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// NEVER CALLED
if (self.isCancelled) {
[connection cancel];
self.receivedData = nil;
return;
}
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{// NEVER CALLED
if (self.isCancelled) {
self.receivedData = nil;
return;
}
// return data to the delegate
}
问题是虽然main方法被称为didReceiveData neverGetsCalled。
我在这里开始这个操作:
- (void)main
{
@autoreleasepool {
if (self.isCancelled) {
return;
}
NSOperationGetJSONFromWeb *webGetOp = [[NSOperationGetJSONFromWeb alloc] initWithURLString:self.urlStr andDelegate:self];
[self.opQueue addOperation:webGetOp];
}
}
这显然来自另一个NSOperation。 这里有什么问题? 如果我直接调用此操作(而不是从另一个操作),一切正常。
答案 0 :(得分:0)
使用KVO标志,如
#pragma mark - OVERRIDE
- (void)main
{
@autoreleasepool {
if (self.isCancelled) {
return;
}
NSURL *url = [NSURL URLWithString:self.urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self willChangeValueForKey:@"isExecuting"];
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
executing = YES;
[self didChangeValueForKey:@"isExecuting"];
}
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// NEVER CALLED
if (self.isCancelled) {
[connection cancel];
self.receivedData = nil;
return;
}
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{// NEVER CALLED
[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"];
executing = NO;
finished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
if (self.isCancelled) {
self.receivedData = nil;
return;
}
// return data to the delegate
}