NSURLConnection不会在多个视图中调用完成 [英] NSURLConnection does not call complete across multiple Views

查看:122
本文介绍了NSURLConnection不会在多个视图中调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候,我提出了以下问题: iOS块正在被窃取查看推送



我提到的操作(OP1)实际上是一个http get到我的服务器,使用NSURLConnection。

之后更多的调查,我发现块实际上并不死。真正发生的是,即使在推送视图(通过[NSThread sleep:10]验证)后,请求实际上是SENT(服务器端记录它)。服务器响应,但是如果view2被推送,应用程序端会发生NOTHING!几乎好像连接已经失去了代表!另一种可能性是NSURLConnection是否在 rsMainLoop 相关?



任何人都可以帮助?



请勿忘记:

0.只要在操作完成之前不推送view2,一切正常。

1.请求发送异步

2.我设置委托,只要视图不变,就可以工作

3.查看 1 使用单例对象参考属性OP1Completed

4.视图 2 通过单例对象引用的属性检查OP1的完成

5。查看 2 通过转到singleton.OP1Result属性获取结果



编辑1: br>
好​​吧,有一些代码。首先这是我的单身人士的相关代码(名为交互):

   - (void)loadAllContextsForUser:(NSString *)用户名{
userNameAux = username;
_loadingContextsCompleted = NO;
if(contextsLoaderQueue == NULL){
contextsLoaderQueue = dispatch_queue_create(contextsLoaderQueue,NULL);
}

dispatch_async(contextsLoaderQueue,^ {
NSLog(@加载所有上下文块开始);
[self requestConnectivity];

dispatch_async(dispatch_get_main_queue(),^ {
[Util Get:[NSString stringWithFormat:@%@ / userContext?username =%@,Util.azureBaseUrl,[username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
successBlock:^(NSData * data,id jsonData){
NSLog(@加载所有上下文块成功);
if([userNameAux isEqualToString:username]){
_allContextsForCurrentUser = [[ NSSet alloc] initWithArray:jsonData];
}
} errorBlock:^(NSError * error){
NSLog(@%@,error);
} completeBlock: {
NSLog(@加载用户异步块的所有上下文已完成);
_loadingContextsCompleted = YES;
[self releaseConnectivity];
}];
} );

while(!_loadingContextsCompl e)){
NSLog(@加载所有上下文块等待);
[NSThread sleepForTimeInterval:.5];
}
});
NSLog(@加载所有上下文调度,如果还没有,应该随时开始。);
}

这里是Util类,它实际上处理了请求/响应< ($ const $)$($)$($)$($)$($)$($)$($) completeBlock; {
if(self = [super init]){
_data = [[NSMutableData alloc] init];
}

_successBlock = [successBlock copy];
_completeBlock = [completeBlock copy];
_errorBlock = [errorBlock copy];

NSURL * url = [NSURL URLWithString:resourceURL];
NSMutableURLRequest * request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
// [_ conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
// [_ conn start];
NSLog(@请求开始);

return self;


- (void)connection:(NSURLConnection *)连接didReceiveResponse :( NSURLResponse *)响应
{
[_data setLength:0];
}

- (void)connection:(NSURLConnection *)连接didReceiveData:(NSData *)data
{
[_data appendData:data];
}
- (void)connectionDidFinishLoading :( NSURLConnection *)连接
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];

id key = [[jsonObjects allKeys] objectAtIndex:0];
id jsonResult = [jsonObjects objectForKey:key];

_successBlock(_data,jsonResult);
_completeBlock();


- (void)连接:(NSURLConnection *)连接didFailWithError:(NSError *)错误
{
_errorBlock(error);
_completeBlock();
}

最后这里是VC1的相关部分(推入VC2)

   - (IBAction)loginClicked {
NSLog(@clicked to exibit next view);

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@MainStoryboard_iPhonebundle:nil];
AuthenticationViewController * viewController =(AuthenticationViewController *)[storyboard instantiateViewControllerWithIdentifier:@ContextSelectionView];

NSLog(@准备完成,立即推送视图);

[self presentViewController:viewController animated:YES completion:nil];
}


解决方案

我已经弄清楚了。在我的第二个视图(我的w8的操作完成)我不能w8使用ThreadSleep!我必须使用 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];


Earlier today I asked the following question: iOS block being stoped when view pushed

The operation I mentioned (OP1) is actually a "http get" to my server, using NSURLConnection.
After even more investigation I discovered that the block doesn't actually "die". What really happens is that the request is actually SENT (the server side logs it), even after the view is pushed (verified via [NSThread sleep:10]). The server responds but then NOTHING happens on the app side if the view2 has been pushed! almost as if the connection had lost its delegate! Another possibility im looking at is "the fact that NSURLConnection is on the rsMainLoop related?"

Can anyone help?

Pls don't forget that:
0. Everything works fine as long as the view2 is not pushed until operation completion.
1. The request is sent async
2. I set the delegate and it works as long as the view dont change
3. view1 starts the operation using the "singleton object reference" property "OP1Completed"
4. view2 checks the completion of OP1 via propertie on the "singleton object reference"
5. view2 gets the "result" by going to the "singleton.OP1Result" property

Edit 1:
Ok lets have some code. First here is the relevant code of my singleton (named "Interaction"):

-(void)loadAllContextsForUser:(NSString *)username{
userNameAux = username;
_loadingContextsCompleted = NO;
if (contextsLoaderQueue == NULL) {
    contextsLoaderQueue = dispatch_queue_create("contextsLoaderQueue", NULL);
}

dispatch_async(contextsLoaderQueue, ^{
    NSLog(@"Loading all contexts block started");
    [self requestConnectivity];

    dispatch_async(dispatch_get_main_queue(), ^{
        [Util Get:[NSString stringWithFormat:@"%@/userContext?username=%@", Util.azureBaseUrl, [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
     successBlock:^(NSData *data, id jsonData){
         NSLog(@"Loading all contexts block succeeded");
         if([userNameAux isEqualToString:username]){
             _allContextsForCurrentUser = [[NSSet alloc]initWithArray: jsonData];
         }
     } errorBlock:^(NSError *error){
         NSLog(@"%@",error);
     } completeBlock:^{
         NSLog(@"load all contexts for user async block completed.");
         _loadingContextsCompleted = YES;
         [self releaseConnectivity];
     }];
    });

    while (!_loadingContextsCompleted) {
        NSLog(@"loading all contexts block waiting.");
        [NSThread sleepForTimeInterval:.5];
    }
});
NSLog(@"Load All Contexts Dispatched. It should start at any moment if it not already.");
}

And here is the class Util, which actually handles the request/response

-(id)initGet:(NSString *)resourceURL successBlock:(successBlock_t)successBlock errorBlock:(errorBlock_t)errorBlock completeBlock:(completeBlock_t)completeBlock;{
if(self=[super init]){
    _data=[[NSMutableData alloc]init];
}

_successBlock = [successBlock copy];
_completeBlock = [completeBlock copy];
_errorBlock = [errorBlock copy];

NSURL *url = [NSURL URLWithString:resourceURL];
NSMutableURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
//[_conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
//[_conn start];
NSLog(@"Request Started.");

return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [_data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];

id key = [[jsonObjects allKeys] objectAtIndex:0];
id jsonResult = [jsonObjects objectForKey:key];

_successBlock(_data, jsonResult);
_completeBlock();
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
_errorBlock(error);
_completeBlock();
}

And finally here is the relevant part VC1 (pushing in VC2)

- (IBAction)loginClicked {
NSLog(@"login clicked. Preparing to exibit next view");

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
AuthenticationViewController *viewController = (AuthenticationViewController *)[storyboard instantiateViewControllerWithIdentifier:@"ContextSelectionView"];

NSLog(@"Preparation completed. pushing view now");

[self presentViewController:viewController animated:YES completion:nil];
}

解决方案

I've figured it out. In my second view (where i w8 for the operation complete) I cannot w8 using ThreadSleep! I have to use [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

这篇关于NSURLConnection不会在多个视图中调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆