请帮助我这个如何在iphone的后台启动线程,每次当应用程序在我的sqlite数据库表中启动时,我有一个列同步状态.there,我有3个值0,1,3。我必须调用数据库表上的这些值。0意味着如果应用程序有同步数据到server.if,我的数据库中的任何数据都会更新,同步状态更改为1和3意味着数据正在发送到服务器,即同步正在进行中。
发布于 2011-06-29 19:32:03
//创建请求
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的委托方法
- (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];
}希望这能有所帮助
发布于 2011-06-29 19:36:58
您可以使用NSThread类方法来启动新的线程。
[NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
///////
-(void)newThread{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
///your code
[pool release];
}有关更多信息,请阅读Thread Programming guide
发布于 2011-06-29 19:37:28
在后台启动线程很容易用NSInvocation完成,你需要知道的就是here
GCD是有的,但各有各的
https://stackoverflow.com/questions/6519840
复制相似问题