- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用performselectorinbackground,但它没有运行。有人告诉我,应用程序设置中有一项检查允许 ios 中的后台任务,但我找不到该检查。因此,请告诉我如何通过将检查添加到应用程序设置 plist 文件来打开后台任务功能。我正在使用此代码进行后台任务。
@implementation NetworkHandler
@synthesize delegate=_delegate;
- (void)sendServerCall:(NSDictionary *)data {
[self performSelectorInBackground:@selector(onBackground:) withObject:data];
}
-(void)onBackground:(NSDictionary *)data
{
@try{
_methodNameResult=[NSString stringWithFormat:@"%@%@", [data objectForKey:Dictionary_Keys_Method],Dictionary_Keys_ResultVal];
_delegateKey=[[data objectForKey:Dictionary_Keys_Delegate] integerValue];
NSURL *serviceUrl = [NSURL URLWithString:[data objectForKey:Dictionary_Keys_WebServiceUrl]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[[data objectForKey:Dictionary_Keys_SopaMessage] length]];
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:[NSString stringWithFormat:@"%@%@", [data objectForKey:Dictionary_Keys_WebServiceNameSpace],[data objectForKey:Dictionary_Keys_Method]] forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[[data objectForKey:Dictionary_Keys_SopaMessage] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self ];
if ( theConnection ){
_conWebData= [NSMutableData data];
}
else
{
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
}@catch (NSException *exception) {
[CommonMethods logException:@"NetworkHandler - onBackground\n" :exception :@"NetworkHandler"];
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
}
-(void)onMain:(NSDictionary *)data
{
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response{
@try{
[_conWebData setLength:0];
}@catch (NSException *exception) {
[CommonMethods logException:@"NetworkHandler - didReceiveResponse\n" :exception :@"NetworkHandler"];
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{
@try{
[_conWebData appendData:data];
}@catch (NSException *exception) {
[CommonMethods logException:@"NetworkHandler - didReceiveData\n" :exception :@"NetworkHandler"];
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error{
@try{
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}@catch (NSException *exception) {
[CommonMethods logException:@"NetworkHandler - didFailWithError\n" :exception :@"NetworkHandler"];
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection{
@try{
NSString*theXML = [[NSString alloc]initWithBytes: [_conWebData mutableBytes]length:[_conWebData length]encoding:NSUTF8StringEncoding];
NSError *parseError = nil;
NSDictionary *data=[[NSDictionary alloc]init];
data=[XMLReader dictionaryForXMLString:theXML error:&parseError];
[self performSelectorOnMainThread:@selector(onMain:) withObject:data waitUntilDone:true];
}@catch (NSException *exception) {
[CommonMethods logException:@"NetworkHandler - connectionDidFinishLoading\n" :exception :@"NetworkHandler"];
[self performSelectorOnMainThread:@selector(onMain:) withObject:[[NSDictionary alloc]init] waitUntilDone:true];
}
}
我不知道,但我认为我不需要在这里执行performSelectorInbackground,因为我认为我在这里执行服务器调用,而performSelectorInbackground基本上用于内部计算或从内部数据库获取数据时使用,所以不需要使用performSelectorInbackground,但如果否则,如果我需要使用 PerformSelectorInbackground 那么我们如何启用此功能并自动启用???
最佳答案
performSelectorInBackground 和performSelectorOnMainThread 分别与在后台和主线程上执行操作(执行函数)有关。这两个函数都与线程概念相关。
performSelectorInBackground,在后台线程上执行选择器内的指令。当应用程序处于前台状态时,这会起作用。
如果您想在应用程序处于后台状态时执行任何任务,那么在这种情况下,performSelectorInBackground 将不起作用。
如果您想在后台执行任务,那么以下链接可能会很有用 -
关于ios - 如何打开 performselectorinbackground 功能 Ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28556441/
performSelectorInBackground 是否为每个调用生成一个新线程,还是为所有调用(可能排队)共享一个线程(不是主线程)? 最佳答案 每次调用 -performSelectorInB
在 View 中,我显示从 coredata 获取的图像,我使用轮播类显示的图像。在同一 View 中,我有 Facebook 按钮,它将导航到另一个屏幕。当我单击 Facebook 按钮时,不会执行
我有一个 Action ,应该制作一些动画,在这些动画期间,我想推送另一个 View Controller ,但我想保持动画运行...我的想法是使用 performSelectorInBackgrou
抱歉新手问题(也许)。我正在为 ios 开发一个应用程序,我正在尝试从主线程中执行一个外部 xml 读取,以便在调用发挥其魔力时不卡住 ui。 这是我所知道的使进程不在 objective-c 的主线
我正在调用一个在后台线程中运行的方法: [self performSelectorInBackground:@selector(loadViewControllerWithIndex:) withOb
即使用户最小化并开始在 ios 设备上处理其他内容,我也试图让应用程序在后台任务上运行,但只有当应用程序在屏幕上处于事件状态时,我的方法才会被命中。我在这里遗漏了一些明显的东西吗? -(void) v
我想在后台调用这个方法, -(void)downloadImage_3:(NSString* )Path AtIndex:(int)i 我以这种方式调用,但它崩溃并显示 EXC_BAD_ACCESS
好吧,我遇到了一个看似非常简单的情况,但无法弄清楚。 在我的 viewDidLoad 中,我有: -(void)viewDidLoad { [super viewDidLoad]; W
简单的问题:如果我这样做会发生什么: - (void)viewDidLoad { [self performSelectorInBackground:@selector(myBGMethod)
我有一个类 MatchDayDataController ,有一个方法 pushIncompleteDataToServer。 在另一个类 SummaryVC.m 中,我想在 performSelec
我需要对某个网址进行多次 https 调用。因此我做了这样的事情 //ViewController Source -(IBAction) updateButton_tapped { [self
我是 iOS 开发新手。我在运行后台线程时遇到问题。在我的代码中,resetUi 在主 UI 线程上运行,现在我正在启动一个后台线程来获取图像数据并更新我的图像。一切正常,但在调用 performSe
如何使用 performSelectorInBackground 调用具有多个参数的方法,如下所示? 示例方法: -(void) reloadPage:(NSInteger)pageIndex fir
我有一种方法用于在用户单击“保存”时保存图像。我使用 PerformSelectorInBackground 保存图像,弹出 View Controller 并显示上一个 View Controlle
我一直在许多应用程序中使用 -performSelectorInBackground,有点忘记了 -detachNewThreadSelector。现在我想知道两者之间有什么区别。它们是否几乎可以互换
performSelectorInBackground:中有performSelector: afterDelay:和NSObject方法。如何将它们合并为一行代码? 也许以某种方式与NSTimer在
我正在尝试使用performselectorinbackground,但它没有运行。有人告诉我,应用程序设置中有一项检查允许 ios 中的后台任务,但我找不到该检查。因此,请告诉我如何通过将检查添加到
我正在尝试通过在后台运行的功能更改我的 MPMoviePlayerController 方法的内容。但是我的内容没有更改。 [self performSelectorInBackground:@sel
我在 iOS 中使用 MapKit 和注释。在添加注释之前(这可能需要一段时间),我想显示一个小的“正在加载...”UIView。如果我在没有线程的情况下执行此操作,则加载 View 会滞后很多,以至
我有一个 UITableView,当项目被选中时,它加载一个 viewController,在它内部使用 performSelectorInBackground 在后台执行一些操作。 如果您慢慢点击
我是一名优秀的程序员,十分优秀!