brain-dead NSThread question ...
brain-dead NSThread question ...
- Subject: brain-dead NSThread question ...
- From: John Michael Zorko <email@hidden>
- Date: Sun, 14 Sep 2008 16:12:19 -0700
Hello, all ...
I'm trying to launch a thread to connect to a server and stream some
data. What i'm seeing is that, while my thread launches, the
NSURLConnection callbacks are never called, and it's probably some
silly thing i'm not understanding about ObjC / Cocoa (being an old C /
C++ developer):
int keepRunningThread = 0;
- (void)playStream:(Song *)streamToPlay
{
// if a player thread is running, tell it to stop ...
keepRunningThread = 0; // tell thread to stop
while(myData.playing == YES) // wait for it to stop
{
sleep(0);
}
// ... and start a new one with the new stream to play
NSLog(@"starting thread to play mp3 stream %s", [streamToPlay.mp3
UTF8String]);
[NSThread detachNewThreadSelector:@selector(connectAndPlayStream:)
toTarget:self withObject:streamToPlay];
}
- (void)connectAndPlayStream:(Song *)streamToPlay
{
myData.playing = YES;
keepRunningThread = 1;
NSString *url = @"http://some_host.com/";
url = [url stringByAppendingString:streamToPlay.mp3];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:[url
stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
if (connection)
{
receivedData = [[NSMutableData data] retain];
}
else
{
NSLog(@"can't connect to server");
}
// I suspect this loop is the problem. How do I wait for all
data to be received without blocking the NSURLConnection callbacks?
while (keepRunningThread)
{
sleep(1);
}
myData.playing = NO;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:
(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data
{
unsigned int length = [data length];
NSLog(@"recv'd %d bytes", length);
[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:NSErrorFailingURLStringKey]);
}
- (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];
}
Regards,
John
Falling You - exploring the beauty of voice and sound
http://www.fallingyou.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden