今天早些时候,我问了以下问题:iOS 块在视图推送时被停止
我提到的操作(OP1)实际上是对我的服务器的“http get”,使用 NSURLConnection。
经过更多调查后,我发现该块实际上并没有“死亡”。真正发生的是请求实际上是发送的(服务器端记录它),即使在视图被推送之后(通过 [NSThread sleep:10] 验证)。服务器响应,但如果 view2 已被推送,则应用程序端不会发生任何事情!几乎就好像连接失去了它的代表!我正在研究的另一种可能性是“NSURLConnection 与rsMainLoop相关的事实?”
任何人都可以帮忙吗?
请不要忘记:
0。只要在操作完成之前不推送 view2,一切正常。
1. 请求是异步发送的
2. 我设置了委托,只要视图不改变它就可以工作
3. 视图1使用“单例对象引用”属性“OP1Completed”开始操作
4. 视图2检查 OP1 的完成情况通过“singleton object reference”上的属性
5. 视图2通过转到“singleton.OP1Result”属性获取“结果”
编辑1:
好的,让我们有一些代码。首先是我的单例的相关代码(名为“交互”):
-(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.");
}
这是 Util 类,它实际上处理请求/响应
-(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();
}
最后是相关部分 VC1(推入 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];
}