使用NSobject子类进行objective-c线程处理

时间:2011-07-22 13:48:38

标签: objective-c multithreading uiviewcontroller nsobject

我使用了一个线程类(下面是.h / .m),其中子类是UIViewcontroller,没有任何问题。

@interface myFirstClass : UIViewController <MyOperationDelegate>{

但是当我使用子类是一个NSobject来调用一个可访问性类来检查互联网连接时,应用程序在调用performSelectorOnMainThread时崩溃了?我不明白为什么,当我构建应用程序时没有错误,当它崩溃时,我得到的是EXC_BAS_ACCESS。在处理NSObject时是不是可以这样做?任何建议对我都有帮助。

@interface AppController : NSObject <MyOperationDelegate>{

myThreading.h

@protocol MyOperationDelegate
    @required
    -(void) updatedStatus:(NSArray*)items;
    -(void) failedStatusWithError:(NSError*)error;
@end

@interface MyOperation : NSObject {    
    NSObject<MyOperationDelegate> * delegate;
    NSOperationQueue *queue;
}

@property (retain) NSObject<MyOperationDelegate> *delegate;

-(void)load: (NSString *)stringUrlPath:(NSString *)functionAction;

@end

myThreading.m

@interface MyOperation (NSObject)
    -(void)dispatchLoadingOperation:(NSDictionary *)aParameters;        
@end


@implementation MyOperation

@synthesize delegate;

-(id)init   
{
    if ([super init]!=nil) {
        queue = [NSOperationQueue new];
        [queue setMaxConcurrentOperationCount:1];
    }
    return self;
}

-(void)load: (NSString *)stringUrlPath: (NSString *)functionAction {

    [self dispatchLoadingOperation:[NSDictionary dictionaryWithObjectsAndKeys:
                                        stringUrlPath, @"urlString", functionAction, @"action", nil]];
}

-(void)dealloc {
        [queue cancelAllOperations];
        self.delegate = nil;
        [super dealloc];
}

-(void)dispatchLoadingOperation:(NSDictionary *)aParameters {

    if([aParameters objectForKey:@"action"] == @"getStatus"){
        @synchronized(self) {
            NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                                        selector:@selector(fetchCheckStatus:)
                                                                                          object:aParameters];
            [queue addOperation:operation];
            [operation release];
        }
    }
}


-(void) fetchCheckStatus:(NSDictionary *)aParameters
{   

    NSData* data = [[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:[aParameters objectForKey:@"urlString"]] ];

    NSError *error;
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    if (responseString != nil) {

        NSMutableArray *rssItems;

        [self.delegate performSelectorOnMainThread:@selector(updatedStatus:) withObject:[NSArray arrayWithObjects:rssItems, nil] waitUntilDone:NO];


    } else {
            [queue cancelAllOperations];
            [self.delegate performSelectorOnMainThread:@selector(failedStatusWithError:) withObject:error waitUntilDone:NO];
    }

    [responseString autorelease];
    [data release];
}
@end 

1 个答案:

答案 0 :(得分:0)

问题是这些问题:

NSMutableArray *rssItems;

[self.delegate performSelectorOnMainThread:@selector(updatedStatus:) withObject:[NSArray arrayWithObjects:rssItems, nil] waitUntilDone:NO];

您声明变量rssItems但不设置它。它将包含来自堆栈的随机垃圾,然后将其解释为指针。也许有时候你很幸运,价值实际上是一个指向生物的指针,但更可能取消引用它会导致你的崩溃。

您需要实际初始化变量,例如:

NSMutableArray *rssItems = nil;

但我猜你真的想要:

NSMutableArray *rssItems = [NSMutableArray array];