Results 1 to 2 of 2
  1. #1
    Problem - Functions looping for no reason

    Member Since
    Mar 18, 2009
    Posts
    2
    Problem - Functions looping for no reason
    I am a very new developer to Objective-C. I have no idea what I'm doing. I wrote this program as a proof of concept GUI command line wrapper. The compressZip and compressTar functions work fine, and the initComp when called from compressZip works fine as well same with initTar respectively. However, when finishedTar (which is called by initTar) calls either compressBZip2 or compressGZip, the functions start looping, and it's not only the compress functions. The finishedCompress starts looping along with the initComp function. I already made a version where NSLog logged every start and end of a function but I couldn't figure out a pattern (not saying much though). This version is clean and has no NSLogs. If you have the time, please take a look (I mean a lot of time - I read the source for hours!). Sorry if this is confusing.
    NOTE: DO NOT RUN THE BZIP2 OR GZIP COMPRESSION WITHOUT CONSTANT MONITORING AND XCODE AT THE READY TO KILL THE TASK. IT WILL COMPLETELY LOCK UP YOUR COMPUTER IF LEFT RUNNING ON THOSE TWO TASKS FOR MORE THAN 20SECS (WON'T EVEN LET YOU FORCE QUIT).

    Code:
    //AppController.h
    #import <Cocoa/Cocoa.h>
    
    @interface AppController : NSObject {
        IBOutlet id cmdCompress;//PushButton
        IBOutlet id mnuCompression;//Popup Menu (NSPopupButton)
        IBOutlet id progressBar;//Progress Indicator
        IBOutlet id txtPath;//Text box
    	NSTask *tarFile, *compFile;
    	NSString *targetPath, *archiveName;
    	NSInteger compressionType;
    }
    - (IBAction)compressFiles:(id)sender;
    - (id)compressTar;
    - (id)compressZip;
    - (id)compressBZip2;
    - (id)compressGZip;
    - (id)initTar;
    - (id)initComp;
    - (void)finishedTar:(NSNotification *)aNotification;
    - (void)finishedCompress:(NSNotification *)aNotification;
    @end
    
    /////////////////////////////////
    //AppController.m
    #import "AppController.h"
    
    @implementation AppController
    - (IBAction)compressFiles:(id)sender {
        [cmdCompress setTitle:@"Running..."];
    	[cmdCompress setEnabled:NO];
    	[progressBar startAnimation: self];
    	targetPath = [[txtPath stringValue] stringByExpandingTildeInPath];
    	compressionType = [mnuCompression indexOfSelectedItem];
    	if (compressionType == 0) {
    		[self compressZip];
    	}
    	else {
    		[self compressTar];
    	}
    }
    - (id)compressZip {
    	[self initComp];
    	archiveName = [targetPath stringByAppendingString:@".zip"];
    	compFile = [[NSTask alloc] init];
    	[compFile setLaunchPath:@"/usr/bin/zip"];
    	[compFile setCurrentDirectoryPath:@"/"];
    	[compFile setArguments:[NSArray arrayWithObjects:@"-r",archiveName,targetPath,nil]];
    	[compFile launch];
    	return self;
    }
    
    - (id)compressTar {
    	[self initTar];
    	archiveName = [targetPath stringByAppendingString:@".tar"];
    	tarFile = [[NSTask alloc] init];
    	[tarFile setLaunchPath:@"/usr/bin/tar"];
    	[tarFile setCurrentDirectoryPath:@"/"];
    	[tarFile setArguments:[NSArray arrayWithObjects:@"-cf",archiveName,targetPath,nil]];
    	[tarFile launch];
    	return self;
    }
    - (id)compressBZip2 {
    	[self initComp];
    	compFile = [[NSTask alloc] init];
    	[compFile setLaunchPath:@"/usr/bin/bzip2"];
    	[compFile setCurrentDirectoryPath:@"/"];
    	[compFile setArguments:[NSArray arrayWithObject:archiveName]];
    	[compFile launch];
    	return self;
    }
    - (id)compressGZip {
    	[self initComp];
    	compFile = [[NSTask alloc] init];
    	[compFile setLaunchPath:@"/usr/bin/gzip"];
    	[compFile setCurrentDirectoryPath:@"/"];
    	[compFile setArguments:[NSArray arrayWithObject:archiveName]];
    	[compFile launch];
    	return self;
    }
    
    - (id)initTar {
    	self = [super init];
        [[NSNotificationCenter defaultCenter] addObserver:self 
    											 selector:@selector(finishedTar:) 
    												 name:NSTaskDidTerminateNotification 
    											   object:nil];
    	
    	tarFile = nil; // This is a good time to initialize the pointer
    	return self;
    }
    - (id)initComp {
    	self = [super init];
        [[NSNotificationCenter defaultCenter] addObserver:self 
    											 selector:@selector(finishedCompress:) 
    												 name:NSTaskDidTerminateNotification 
    											   object:nil];
    	
    	compFile = nil; // This is a good time to initialize the pointer
    	return self;
    }
    
    - (void)finishedTar:(NSNotification *)aNotification {
    	[tarFile release];
    	tarFile = nil;
    	if (compressionType == 1) {
    		[self compressBZip2];
    	}
    	else if (compressionType == 2) {
    		[self compressGZip];
    	}
    }
    - (void)finishedCompress:(NSNotification *)aNotification {
    	[compFile release];
    	compFile = nil;
    	[progressBar stopAnimation: self];
    	[cmdCompress setEnabled:YES];
    	[cmdCompress setTitle:@"Compress Files"];
    }
    @end

  2. #2
    Problem - Functions looping for no reason

    Member Since
    Mar 18, 2009
    Posts
    2
    Fixed it!
    sorry, I got it.
    Can't use two NSNotification
    Just used one and an if statement.
    Thanks,
    Robert

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Button Looping and me!
    By anonix in forum iOS Development
    Replies: 6
    Last Post: 09-02-2011, 10:47 AM
  2. Iweb looping audio problem?
    By jdhart73 in forum OS X - Apps and Games
    Replies: 0
    Last Post: 09-29-2009, 03:00 AM
  3. Macbook Looping
    By grodrigz in forum Apple Notebooks
    Replies: 0
    Last Post: 06-12-2009, 03:04 AM
  4. problem using Reason 4 music application
    By silas in forum Music, Audio, and Podcasting
    Replies: 2
    Last Post: 04-30-2008, 02:36 AM
  5. Disk Permissions problem with Reason 3.0.5
    By jesusholdon in forum OS X - Apps and Games
    Replies: 2
    Last Post: 02-22-2007, 04:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •