未使用block-iOS保留变量
我不明白为什么伯爵没有留在街区外。有什么帮助吗?使用block 你也可以利用block的优势 创建块方法以从服务器获取响应。下面我修改了方法签名以使用块调用:未使用block-iOS保留变量,ios,mobile,Ios,Mobile,我不明白为什么伯爵没有留在街区外。有什么帮助吗?使用block 你也可以利用block的优势 创建块方法以从服务器获取响应。下面我修改了方法签名以使用块调用: 2014-11-20 13:45:52.667 myFriday[2888:81062] HR 0 2014-11-20 13:45:52.879 myFriday[2888:81062] HR1 10 首先,完成块不会立即执行。从日志的顺序可以看出,块内的NSLog在底部的日志之后执行。当底部的NSLog执行responseArray
2014-11-20 13:45:52.667 myFriday[2888:81062] HR 0
2014-11-20 13:45:52.879 myFriday[2888:81062] HR1 10
首先,完成块不会立即执行。从日志的顺序可以看出,块内的
NSLog
在底部的日志之后执行。当底部的NSLog执行responseArray
时,仍然是nil
。我需要将responseArray.count传递给另一个控制器(现在为0)。解决方案是什么?有什么帮助吗?如果您仍要阻止线程,那么使用sendSynchronousRequest:returningResponse:error:
不仅更容易吗?不管怎样,你都不想从主线程开始。对!这是双方的瓶颈。让我也提供块解决方案。我尝试使用信号量。实际上,当单击屏幕上的通知按钮时,会调用fetchDataForNotifications方法。在使用信号量之后,当我单击按钮时,什么也没有发生,UI被阻塞。
2014-11-20 13:45:52.667 myFriday[2888:81062] HR 0
2014-11-20 13:45:52.879 myFriday[2888:81062] HR1 10
- (void) fetchDataForNotifications:(void (^)(NSMutableArray *response))block {
__block NSMutableArray *responseArray = nil;
NSString *requestData = [self prepareRequestJsonForNotificatios];
NSData *postData = [requestData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSString* urlString = [SERVER_URL_QA stringByAppendingString:@"/api/home/notifications"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setTimeoutInterval:10.0];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setHTTPShouldHandleCookies:YES];
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *dataAll, NSError *error)
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
long int httpResponseCode = [httpResponse statusCode];
if (error == nil && httpResponseCode == 200 && dataAll && dataAll.length > 0){
NoticationsModel *notificationRow = [[NoticationsModel alloc]init];
responseArray = [NSJSONSerialization JSONObjectWithData:dataAll options:NSJSONReadingMutableContainers error:nil];
NSLog(@"HR1 %d", responseArray.count);
for(int i =0;i<responseArray.count;i++){
notificationRow.fromPhotoURL = [[responseArray objectAtIndex:i] valueForKey:@"Fromphotourl"];
notificationRow.notificationBody = [responseArray valueForKey:@"Body"];
notificationRow.updatedDate = [[responseArray objectAtIndex:i] valueForKey:@"UpdatedDate"];
notificationRow.fromName = [responseArray valueForKey:@"FromName"];
[notificationArray addObject:notificationRow];
}
}
else {
NSString* errorMessage = [NSString stringWithFormat:@"Data retrived could not be parsed. Error code %ld", (long)error.code ];
NSLog(@"%@",errorMessage);
}
block(responseArray);
}];
}
[self fetchDataForNotifications:^(NSMutableArray *response) {
// Perform operation on main thread.
// Don't forget to set response array value to tableview datasource array.
// e.g. self.tableArray = response;
[self performSelectorOnMainThread:@selector(realoadTableData) withObject:nil waitUntilDone:YES];
}];
- (void) realoadTableData {
[self.tableView reloadData];
}