Re: iOS4: CFReadStream and background operation
Re: iOS4: CFReadStream and background operation
- Subject: Re: iOS4: CFReadStream and background operation
- From: John Michael Zorko <email@hidden>
- Date: Wed, 23 Jun 2010 13:29:40 -0700
Jens,
>> 2. However, CFReadStreams created _while_ the app was in the background often (though not always) _don't_ call their callbacks _until_ the app is brought in the foreground, then all of a sudden the callback happens.
>
> I think foreground/background is a red herring — that has almost no effect on the behavior of the app.
> What I _have_ seen is bugs in my code that result in incoming activity (like network I/O) not waking up the runloop, so the event doesn’t get delivered … until some other event comes in and causes the runloop to wake up. This is a lot likelier to happen when the app is in the background, because when it’s active there are user events (even the mouse entering/exiting views) that keep the runloop going pretty often.
Thanks for the reply and the info :-) I still think it has to do with foreground / background, because i've narrowed it down to CFReadStreamRead() hanging (as if there were no bytes to read) even though the read stream callback got the kCFStreamEventHasBytesAvailable event _and_ CFReadStreamHasBytesAvailable() returns TRUE (I wanted to make bloody sure). The section of code is _literally_ this (from my CFReadStream callback):
} else if (eventType == kCFStreamEventHasBytesAvailable) {
//
// Read the bytes from the stream
//
BOOL reallyReallyHasBytes = CFReadStreamHasBytesAvailable(stream);
NSLOG(@"really really has bytes: %s", reallyReallyHasBytes ? "yes" : "no");
UInt8 bytes[kAQBufSize];
CFIndex length = CFReadStreamRead(stream, bytes, kAQBufSize);
NSLOG(@"recvd %i bytes", length);
Output:
2010-06-23 13:10:23.299 AudioPress[933:a61f] Thread 94518032: AudioEngineShoutcast 3959968: really really has bytes: yes
(and no more output ... it's hung on the CFReadStreamRead)
... and this code _works_ when in the foreground, but when in the background, the CFReadStreamRead() often (though not always) hangs. This is already after the run loop dispatched the event, so AFAIK the code isn't losing events. The documentation for CFReadStream clearly says that if at least one byte is available, CFReadStreamRead() won't block ... yet here it is, blocking (the "recvd bytes" message is never logged and the thread is hung until I put the app in the foreground again). This is using the GM seed of iOS4 on my 3GS. I don't know where else to look or what else to think, other than maybe CFReadStreamRead() isn't guaranteed to work while the app is backgrounded and that I should maybe drop back to the BSD send() / recv() calls and hope they do / are? Is there something else I should be looking for that could cause this?
Regards,
John
_______________________________________________
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