首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iphone中为任意函数调用线程

如何在iphone中为任意函数调用线程
EN

Stack Overflow用户
提问于 2011-06-29 19:23:40
回答 3查看 215关注 0票数 0

请帮助我这个如何在iphone的后台启动线程,每次当应用程序在我的sqlite数据库表中启动时,我有一个列同步状态.there,我有3个值0,1,3。我必须调用数据库表上的这些值。0意味着如果应用程序有同步数据到server.if,我的数据库中的任何数据都会更新,同步状态更改为1和3意味着数据正在发送到服务器,即同步正在进行中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-29 19:32:03

//创建请求

代码语言:javascript
复制
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}

然后实现NSURLConnection的委托方法

代码语言:javascript
复制
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
if(!receivedData){
   receivedData = [[NSMutableData alloc]init];
}
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];

// inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

// release the connection, and the data object
[connection release];
[receivedData release];
}

希望这能有所帮助

票数 0
EN

Stack Overflow用户

发布于 2011-06-29 19:36:58

您可以使用NSThread类方法来启动新的线程。

代码语言:javascript
复制
[NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];



///////

-(void)newThread{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   ///your code

   [pool release];

}

有关更多信息,请阅读Thread Programming guide

票数 0
EN

Stack Overflow用户

发布于 2011-06-29 19:37:28

在后台启动线程很容易用NSInvocation完成,你需要知道的就是here

GCD是有的,但各有各的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6519840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档