Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C methods not running
    primarykey
    data
    text
    <p>I'm messing around with using objects to launch background threads, however when I call an objects method to call the method that will spawn a background thread, nothing happens. I'm a bit puzzled as to why, and it looks like the -init function isn't even being called. Anyways, here's what I have:</p> <p>ViewController.h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "Threader.h" @interface ViewController : UIViewController @property(nonatomic, strong) Thread* threadedObject; - (IBAction)StartBackgroundThreadButtonClicked:(id)sender; @end </code></pre> <p>ViewController.m</p> <pre><code>#import "ViewController.h" #import "Threader.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _threadedObject = [[Threader alloc]init]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)StartBackgroundThreadButtonClicked:(id)sender { NSLog(@"Clicked."); [_threadedObject RunInBackground]; } @end </code></pre> <p>Threader.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Threader : NSObject @property(nonatomic) bool IsFinishedRunning; @property(nonatomic) bool IsThreading; //Constructor and Destructor -(id)init; -(void)dealloc; -(void)RunInBackground; -(void)WaitForTenSeconds; @end </code></pre> <p>Threader.m</p> <pre><code>#import "Threader.h" @implementation Threader //constructor -(id)init{ [super init]; if(self != nil) { _IsFinishedRunning = NO; _IsThreading = NO; } return self; } //destructor -(void)dealloc{ [super dealloc]; } //Runs a thread in the background -(void)RunInBackground{ NSLog(@"Initiating thread..."); [self performSelectorInBackground:@selector(WaitForTenSeconds) withObject:nil]; } //Waits for 10 seconds, then sets IsFinishedRunning to YES -(void)WaitForTenSeconds{ NSLog(@"Starting to run in the background."); _IsThreading = YES; sleep(10); _IsFinishedRunning = YES; NSLog(@"Finished running in the background."); } @end </code></pre> <p>When I run the program, this is my output(I clicked the button a few times)<br> 2013-05-17 15:30:57.267 ThreadedObjects Clicked.<br> 2013-05-17 15:30:59.003 ThreadedObjects Clicked.<br> 2013-05-17 15:30:59.259 ThreadedObjects Clicked.<br> 2013-05-17 15:30:59.443 ThreadedObjects Clicked.<br> 2013-05-17 15:30:59.675 ThreadedObjects Clicked. </p> <p>I should be getting messages telling me that the Threader object was created, and that it is preparing to launch a background thread, that the thread has been spawned and then after 10 seconds, that the thread is done running. So, where's my glaring obvious error?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload