使用線程類(.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
它在哪裏崩潰? – DarkDust
當它調用以下** [self.delegate performSelectorOnMainThread:@selector(updatedStatus :) withObject:[NSArray arrayWithObjects:rssItems,nil] waitUntilDone:NO] ** –