• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSFileHandle readInBackground vs threading?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSFileHandle readInBackground vs threading?


  • Subject: NSFileHandle readInBackground vs threading?
  • From: Alexander Bokovikov <email@hidden>
  • Date: Tue, 08 Nov 2011 22:54:52 +0600

Hi, All,

I have a need to read some data from a local socket, which serves for IPC. And data may come very quickly, so (AFAIU) inner socket buffer might overflow, so a portion of data might be lost. I don't see a way how to define an inner system buffer size, so the only I can is to do my best to read from the socket quickly enough. The problem is that I need yet to process the incoming data, not only to read them. Now I'm doing the next:

- (void) readPacket:(NSNotification *)aNotification {
[packetBuffer appendData:[[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem]];
[sockFileHandle readInBackgroundAndNotifyForModes:modesArray];
[self processPacket];
}


where packetBuffer is the storage for incoming data and processPacket is where data are processed.

My question is:

Isn't it better to do it in this way:

- (void) readPacket:(NSNotification *)aNotification {
[packetBuffer appendData:[[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem]];
[sockFileHandle readInBackgroundAndNotifyForModes:modesArray];
[self performSelector:@selector(processPacket) withObject:nil afterDelay:0];
}


Or is the multithreaded processing the only (or at least much better) solution here? If yes, then may I be sure that NSData appendData (see above) will never relocate the initial portion of the data buffer, but only will add new data to the end of buffer? My data processing routine looks like the next:
................
len = [packetBuffer length];
ptr = [packetBuffer bytes];
while (len >= MIN_PACKET_SZ) {
/// doing something with data pointed by ptr->
ptr += _some_value;
len -= _some_value;
}
................


So, I need be sure that once reading the ptr, then increasing it, step by step up to the len value, I'll always have valid data despite of how many times append data will be called in another thread. What about this?

Thanks in advance.

-Alex
_______________________________________________

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


  • Follow-Ups:
    • Re: NSFileHandle readInBackground vs threading?
      • From: Andreas Grosam <email@hidden>
    • Re: NSFileHandle readInBackground vs threading?
      • From: Scott Ribe <email@hidden>
  • Prev by Date: Different deployment target for iPhone & iPad
  • Next by Date: Why does changing a UIScrollView's contentInset change contentOffset?
  • Previous by thread: Re: Different deployment target for iPhone & iPad
  • Next by thread: Re: NSFileHandle readInBackground vs threading?
  • Index(es):
    • Date
    • Thread