2011-07-22 79 views
0

使用線程類(.h/.m以下)的地方,其中子類是UIViewcontroller,沒有任何問題。使用NSobject子類的objective-c線程

@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 
+0

它在哪裏崩潰? – DarkDust

+0

當它調用以下** [self.delegate performSelectorOnMainThread:@selector(updatedStatus :) withObject:[NSArray arrayWithObjects:rssItems,nil] waitUntilDone:NO] ** –

回答

0

的問題是這些線路:

NSMutableArray *rssItems; 

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

聲明變量rssItems,但不設置它。它將包含堆棧中的隨機垃圾,然後將其解釋爲指針。也許有時候你很幸運,價值實際上是指向活着的物體的指針,但更有可能解引用它會導致你的崩潰。

實際上你需要初始化變量,例如:

NSMutableArray *rssItems = nil; 

,但我猜你真的想:

NSMutableArray *rssItems = [NSMutableArray array]; 
+0

什麼是一個愚蠢的錯誤。我甚至不需要這樣做,我只能專注於在將正確的登錄名添加到位之前進行流程工作,並忽略它。感謝您的幫助和時間。 –

+0

沒問題。順便說一句,靜態分析儀應該已經警告過你。偉大的工具。 – DarkDust

+0

我仍然是所有這些東西的新手,所以每天都在學習。我幾乎沒有使用過分析儀,但是當我從錯誤中獲取任何信息時,都不能確保從現在開始。謝謝你的提示。 –