• 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
Help with notifications and NSFileHandle threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help with notifications and NSFileHandle threads


  • Subject: Help with notifications and NSFileHandle threads
  • From: "Wan, Nathan (CIV)" <email@hidden>
  • Date: Fri, 27 Jun 2008 13:15:09 -0700
  • Thread-topic: Help with notifications and NSFileHandle threads

Hi all,

I am new to Objective-C and Cocoa, but I am learning it for what I thought was a pretty small project.  I need some help with the notification system and the run loop in Cocoa.  The end result is supposed to be a program that constantly reads and writes data from a serial port to a local file, but baby step here.  It was suggested that I use NSFileHandle class to make my program more thread safe.  This is a quick test I tried to write out.  I would change the data in the text file, and hopefully something will show up on the console:

PS what's a proper way to work between NSData and NSString?

//CODE

#import <Cocoa/Cocoa.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSString.h>
#import <Foundation/NSNotificationCenter.h>
#import <stdio.h>

@interface test : NSObject

-(void) writeDataReadInBackground:(NSNotification *)notification;
-(void) testWriteData:(NSData *)data;

@end


int main(int argc, char *argv[])
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *aData = [@"Test" dataUsingEncoding: NSASCIIStringEncoding];
    NSFileHandle *in = [NSFileHandle fileHandleForReadingAtPath:@"/Users/_me_/test.txt"];

     if(in == nil)
        printf("somethig wrong\n");

    test *t = [[test alloc] init];

    [[NSNotificationCenter defaultCenter] addObserver: t selector:@selector(writeDataReadInBackground:) name: NSFileHandleDataAvailableNotification object: nil];

    [t testWriteData: aData];

    [stdIn waitForDataInBackgroundAndNotify];

    while(1)
        sleep(5);

    [t release];
    [pool release];

    return 0;
}


@implementation test
- (void) writeDataReadInBackground:(NSNotification *)notification {

    printf("Notification recieved\n");
    }

- (void) testWriteData:(NSData *)data {

        NSFileHandle *stdOut = [NSFileHandle fileHandleWithStandardOutput];

        [stdOut writeData:data];

    }
@end

//END_CODE

Thanks!!

<EOM>
_______________________________________________

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

  • Prev by Date: Why aren't my bindings firing?
  • Next by Date: Re: Extent of NDA concerning iPhone
  • Previous by thread: Re: Why aren't my bindings firing?
  • Next by thread: simple question about passing around pointers to objects
  • Index(es):
    • Date
    • Thread