2

I subclassed NSOperation and implemented the main method. When I added it to an NSOperationQueue, it was not being called. I compared it to sample code and sample projects that do work and could not see what the problem was. Does anyone have any experience with this problem and what causes it, please?

After going crazy trying to find the problem, I decided to start over from scratch, rebuild my class and test it at each step. In the header, I declared it a subclass of NSOperation. In the implementation file, I implemented main and only put a couple of lines: sleep(3); NSLog..;. All good. Each step worked as I started to add properties, even declared delegate, etc.

Finally, in the main, I put code to use NSDirectoryEnumerator. Suddenly, main was not being called at all. Not only that, when I took that code back out, it still does not work!! I tried restarting XCode 4, restarting my machine (not that I know why these might help, but I'm running out of ideas) but all to no avail.

Any clues at all would be most appreciated. Thanks.


Update: I started over (again!) from scratch but this time I assembled the parts in a different order... and now it works! However, in case there is something that I'm doing wrong anyway and I've just been lucky this time round, here is the @interface section as @Andrew suggested:

@class ScanOperation;

@protocol ScanOperationDelegate 
-(void)fileFound:(ScanOperation *)operation;
-(void)finishedScanOperation:(ScanOperation *)operation;
@end

#import <Foundation/Foundation.h>

@interface ScanOperation : NSOperation {
@private
    NSObject <ScanOperationDelegate> *delegate;
    NSURL *rootUrl;
    int totCount, dirCount, filCount, errCount;
    NSTimeInterval elapsed;
}
- (id)initWithScanRootUrl:(NSURL *)startAtUrl;

@property (readwrite, assign) NSObject <ScanOperationDelegate> *delegate;
@property (readonly) int totCount, dirCount, filCount, errCount;
@property (readonly) NSTimeInterval elapsed;
@end
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
stifin
  • 1,390
  • 3
  • 18
  • 28
  • 1
    it would help to see the @interface section to make sure that you subclassed it properly – Andrew May 23 '11 at 19:52
  • I built it back up from scratch but this time in a different order and it works... however, I'll paste in the @interface anyway in case someone notices something that's not right: – stifin May 23 '11 at 21:17
  • Is your `NSOperation` subclass concurrent? `-main` is automatically invoked only if the operation is **not** concurrent. –  May 23 '11 at 21:24
  • "non-cooncurrent" means that I've written my custom subclass and then implemented a `- (void)main` inside the `@implementation` block, right? If so, that is what I did. What is mystifying is why exactly that `main` was called until I made some changes and then, even after I tried to undo those changes, it still wasn't being called. Now it gets called because I started from the simplest possible implementation (my `main` had only `sleep(3); NSLog("I've finished");` and then went from there – stifin May 23 '11 at 21:47
  • I'm guessing your -(void)main method signature might not've been right. Note it's not (int)main() as with the main() of a standard C program. - this is an instance method with a void return and no arguments -(void)main ... – Joshua Nozzi May 23 '11 at 22:47
  • noted! The next time it happens, I will paste the exact code. I appreciate it's difficult to work out what's wrong without the actual code. I thought that perhaps that the way `NSOperation` subclassing works is very "fragile" and that others had experienced something similar and knew what the common cause(s) was (were). – stifin May 24 '11 at 14:49

2 Answers2

1

I had this happen too. As usual it was me: I hadn't initialised the OperationQueue for the Operation to run on.

Stephan
  • 41,764
  • 65
  • 238
  • 329
user1107201
  • 63
  • 1
  • 7
0

I met this question just now. 1) initialize the OperationQueue 2) add your operation to this queue 3) clean your project 4) run