开发者

Strange custom delegate actions

Ok -- this one is weird. I have a singleton class that loads information from an XML file. I am using a delegate definition as follows (I define the delegate in a separate header file to make life easier):

@protocol ResourceClassDelegate <NSObject>

@optional
- (void)picturesDidStartLoading;
- (void)picturesDidFinishLoading;
@end

In the resource file, the delegate is defined corre开发者_StackOverflow社区ctly (I believe):

@property (assign) id<ResourceClassDelegate> delegate;

When using the delegate, the code in the resource class is as follows:

-(void)refreshPiecesOfHistoryWithOperation {

    NSLog(@"Operation Started");
    if ([delegate respondsToSelector:@selector(picturesDidStartLoading)])
        [delegate picturesDidStartLoading];

    self.picturePacks = [HistoryXMLParser loadPicturePacks];

    [self.allPiecesOfHistory removeAllObjects]; 

    // now lets put all of them in one big file...
    for (PicturePack *pp in self.picturePacks) {
        for (int ct = 0; ct < [[pp piecesOfHistory] count] ; ct++) {
            [self.allPiecesOfHistory addObject:(PieceOfHistory *)[[pp piecesOfHistory] objectAtIndex:ct]];          
        }
    }

    NSLog(@"Operation Ended");
    if ([delegate respondsToSelector:@selector(picturesDidFinishLoading)])
        [delegate picturesDidFinishLoading];

}

Now... in the class that is listening to the delegate, it is assigned:

- (void)viewDidLoad {
    [super viewDidLoad];

    // now for the part that makes the loading all happen...
    [[ResourceClass sharedResourceClass] setDelegate:self];
}

And in the listening class, the methods are defined....

#pragma mark ResourceClassDelegate
-(void)picturesDidStartLoading {

    if (loadingActivity == nil)
        loadingActivity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    [self.view addSubview:loadingActivity];
    [loadingActivity setCenter:[self.view center]];
    [loadingActivity startAnimating];

}

-(void)picturesDidFinishLoading {

    if (loadingActivity != nil) {
        [loadingActivity stopAnimating];
        [loadingActivity removeFromSuperview];
    }

    [self.tableView reloadData];
}

Now for the problem... every single time, in the listening class, the method (void)picturesDidFinishLoading is called. The method (void)picturesDidStartLoading never is called.

When I debug the code, in the resource class, the line

if ([delegate respondsToSelector:@selector(picturesDidStartLoading)])
    [delegate picturesDidStartLoading];

never reaches the delegate method call - even if I remove the if statement. The line

if ([delegate respondsToSelector:@selector(picturesDidFinishLoading)])
    [delegate picturesDidFinishLoading];

is always called.

any ideas?


Ok -- I figured it out....

The delegate was nil during the first call. The reason it is nil is because the function using the delegate was called in the source during the init method. The init method was not complete when the first test of the delegate was performed. At this time the delegate was nil because it is not instantiated until the the init method completes. The reason the second test of the delegate worked is because I submitted the process using an NSOperationQueue.

To fix the problem I have to move things around a bit... it's all about the timing!

Well now that was fun....


That's weird, try to remove @optional in the protocol declaration, and see if you get some warnings.

Try to print a log inside the method as well, other than that it looks fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜