2

我刚刚从 ios6 在 ios7 上运行了我的 twitter 应用程序,发现了一些有趣的东西,当 setMaxConcurrentOperationCount >3 时,NSOperation 子类(ConnectOperation)连接中的请求:didFial:由于超时。它在 ios6 中工作得非常好(我现在可以添加任意数量的操作)。请帮助我没有想法!

[[[KPOperationsManager sharedOperationsManager]getMessagesDownloadQueue] setMaxConcurrentOperationCount:1];

ConnectOperation  * op= [[ConnectOperation alloc]initWithEndpoint:@"https://api.twitter.com/1.1/search/tweets.json" andParameters:@"YOLO" andAccount:account andMOC:_mngObjSlaveFetchContext1 andDelegate:self];

ConnectOperation  * op2  =[[ConnectOperation alloc]initWithEndpoint:@"https://api.twitter.com/1.1/search/tweets.json" andParameters:@"APPLE" andAccount:account andMOC:_mngObjSlaveContext andDelegate:self];
ConnectOperation  * op3  =[[ConnectOperation alloc]initWithEndpoint:@"https://api.twitter.com/1.1/search/tweets.json" andParameters:@"HOUSE" andAccount:account andMOC:_mngObjSlaveFetchContext2 andDelegate:self];
ConnectOperation  * op4  =[[ConnectOperation alloc]initWithEndpoint:@"https://api.twitter.com/1.1/search/tweets.json" andParameters:@"EDC" andAccount:account andMOC:_mngObjSlaveFetchContext3 andDelegate:self];
ConnectOperation  * op5  =[[ConnectOperation alloc]initWithEndpoint:@"https://api.twitter.com/1.1/search/tweets.json" andParameters:@"IPHONE" andAccount:account andMOC:_mngObjSlaveFetchContext4 andDelegate:self];

[[[OperationsManager sharedOperationsManager]getMessagesDownloadQueue] addOperation:op];
[[[OperationsManager sharedOperationsManager]getMessagesDownloadQueue] addOperation:op2];
[[[OperationsManager sharedOperationsManager]getMessagesDownloadQueue] addOperation:op3];
[[[OperationsManager sharedOperationsManager]getMessagesDownloadQueue] addOperation:op4];
[[[OperationsManager sharedOperationsManager]getMessagesDownloadQueue] addOperation:op5];


@implementation ConnectOperation
- (void)connectionDidFinishLoading: (NSURLConnection*) cn
{  //parse data: NEVER GETS HERE IN IOS 7 , ONLY FAILS ; }

- (void) connection: (NSURLConnection*) cn didFailWithError: (NSError*) error
{  NSLog(@"FAILS DUE TO TIME OUT");

- (void)start {
 if( isFinished || [self isCancelled] ) { [self done]; return; }
SLRequest *operation = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/search/tweets.json"] parameters:self.parameters];
[operation setAccount:self.account];
// Use the signed request to start a connection
connection = [NSURLConnection connectionWithRequest:[operation preparedURLRequest]
                                                delegate:self];
if (![NSThread isMainThread]) {
    [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];

    return;
}

[self willChangeValueForKey:@"isExecuting"];
isExecuting = YES;
finished = NO;
[self didChangeValueForKey:@"isExecuting"];
[self.connection start];
}

编辑:

经过进一步调查,似乎即使我 setMaxConcurrentOperationCount:1 第四和第五(op4,op5)操作仍然超时!然而,前 3 个操作按预期以串行方式运行并检索数据。此外,如果我 setMaxConcurrentOperationCount>3 没有任何操作完成,它们都会超时。

4

1 回答 1

1

你有没有试过这个:

// keeps the NSOperation alive for the during of the NSURLConnection!
    [self.downloadConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [self.downloadConnection start];

抱歉迟到了,但我刚刚开始研究 IOS。请让我知道它是否有效。

于 2014-01-23T14:01:26.417 回答