gpt4 book ai didi

iphone - 如何在后台iphone中定期调用webservice?

转载 作者:行者123 更新时间:2023-11-28 20:35:32 26 4
gpt4 key购买 nike

我有网络服务,我想从我的 iPhone 应用程序定期调用此服务,以便在应用程序运行时每 1 分钟兑现一次数据。

我使用 NSTimer 来调用调用此服务的函数,但我很不高兴,因为第一次调用确实完成了对第一次调用的数据的解析,然后才继续进行新的调用。那我该怎么做呢?

{
NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 60.0];
NSTimer *t = [[NSTimer alloc] initWithFireDate: d
interval: 10
target: self
selector:@selector(calltimer:)
userInfo:nil repeats:YES];

NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:t forMode: NSDefaultRunLoopMode];
[t release];
}

-(void)calltimer :(id)sender
{
NSLog(@"yessss");

if(!myQueue)
{
myQueue = dispatch_queue_create("supperApp.user1025523.com", NULL);
dispatch_async(myQueue, ^{
[self getData];
});
}
}

-(void)getData
{
webserviceCaller* wsCaller = [[webserviceCaller alloc]initWithTarget:self selector:@selector(parsINVData:)];
[wsCaller getINventoryData:self.username];
[wsCaller release];
}

-(void) parsINVData:(InvData*) ret
{
//save return data in global variable
}

我使用 NSMutableURLRequest 启动请求参数并使用 NSURLConnection 启动连接,所以为什么没有触发对网络服务的调用。

最佳答案

你可以像下面这样添加一个类级别的成员变量:

在.h文件中

{
BOOL finishedParsing;
}

- (void) nsTimerFunctionCall
{
if(!finishedParsing)
{
//Either return, or recall this same function after some time
return;
}

[self parseCode];
}

- (void) parseCode
{
finishedParsing = NO;

//do long processing function
//....

finishedParsing = YES;
}

这样你就可以确保在处理另一个函数调用时不会调用解析代码

关于iphone - 如何在后台iphone中定期调用webservice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690027/

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