gpt4 book ai didi

ios - 如何打开 performselectorinbackground 功能 Ios

转载 作者:行者123 更新时间:2023-11-29 02:17:46 26 4
gpt4 key购买 nike

我正在尝试使用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 将不起作用。

如果您想在后台执行任务,那么以下链接可能会很有用 -

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

http://www.raywenderlich.com/29948/backgrounding-for-ios

关于ios - 如何打开 performselectorinbackground 功能 Ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28556441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com