我无法将我们的云数据库服务的静态库移植到ARC。
我已经进入了编译和运行的阶段,但它永远不会回叫代表。
我有2个类,一个代理类和一个APIOperation。
APIOperation是NSOperation的子类,它使用NSURLConnection从Web API获取数据。
代理有一个NSOperationQueue,基本上是所有APIOperation调用的委托。
使用模式如下:
代码如下:
代理类:
@implementation theProxy
@synthesize callbackSelector,delegate,opQueue;
-(theProxy*)init{
opQueue = [[NSOperationQueue alloc]init];
return self;
}
- (void) apiOperation:(APIOperation*)operation didCompleteWithResult:(NSArray*)theResult{
SEL selector = [operation callbackSelector];
if ([delegate respondsToSelector:selector]) {
NSInvocation* inv = [NSInvocation invocationWithMethodSignature:[[delegate class] instanceMethodSignatureForSelector:selector]];
[inv setTarget:delegate];
[inv setSelector:selector];
theProxy* tmp = self;
[inv setArgument:&tmp atIndex:2];
[inv setArgument:&operation atIndex:3];
[inv setArgument:&theResult atIndex:4];
[inv invoke];
}
}
- (void) apiOperation:(APIOperation*)operation didFailWithError:(NSString*)theError{
if ([delegate respondsToSelector:@selector(API:apiOperation:didFailWithError:)]) {
[delegate API:self apiOperation:operation didFailWithError:theError];
}
}
-(void)cancelAllOperations{
[opQueue cancelAllOperations];
}
- (void)dealloc
{
[opQueue cancelAllOperations];
[opQueue release], opQueue = nil;
delegate = nil;
//[delegate release]; delegate should not be retained.
[super dealloc];
}
APIOperation类(大大简化):
@implementation APIOperation
@synthesize delegate,APIKey,secretKey,debugMode,callbackSelector,successCallbackMethodSignature,errorCallbackMethodSignature,delegateCallbackMethodSignature,tag,APIServer,requestProcessingTime,requestReceivedTime,responseCode,responseMessage,timestamp,requestRoundTripTime,requestStartMicroTime,useSSL;
-(void) main{
receivedData = [NSMutableData data];
connFinished = NO;
// create the connection with the request
// and start loading the data
theConnection=[[NSURLConnection alloc] initWithRequest:[self buildRequest] delegate:self];
if (theConnection) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!connFinished);
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
id pList = [NSPropertyListSerialization propertyListFromData:receivedData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&errorStr];
theResponse = (NSDictionary*) pList;
if ([delegate respondsToSelector:@selector(apiOperation: didCompleteWithResult:)]) {
NSArray* theResultsArray = [theResponse objectForKey:@"payload"];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:successCallbackMethodSignature];
[inv setTarget:delegate];
[inv setSelector:@selector(apiOperation: didCompleteWithResult:)];
KSAPIOperation* tmp = self;
[inv setArgument:&tmp atIndex:2];
[inv setArgument:&theResultsArray atIndex:3];
[inv performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:YES];
}
}
@end
现在正如我所说的那样直到'if([delegate respondsToSelector ...'在connectionDidfinishLoading中行。在那时它总是返回false。现在假设这与ARC有关,我已经检查了代表不是null且值在那里,委托属性也在APIOperation.h中声明为:
@property (unsafe_unretained) id<KSAPIOperationDelegate,NSObject> delegate;
如果我删除了respondsToSelector检查,那么应用程序在main()中崩溃并发生以下回溯:
#0 0x0156b09b in objc_msgSend ()
#1 0xbfffde10 in ?? ()
#2 0x0132d437 in -[NSInvocation invoke] ()
#3 0x013c8e72 in -[NSObject performSelector:withObject:] ()
#4 0x009369ef in __NSThreadPerformPerform ()
#5 0x0139b97f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#6 0x012feb73 in __CFRunLoopDoSources0 ()
#7 0x012fe454 in __CFRunLoopRun ()
#8 0x012fddb4 in CFRunLoopRunSpecific ()
#9 0x012fdccb in CFRunLoopRunInMode ()
#10 0x012b0879 in GSEventRunModal ()
#11 0x012b093e in GSEventRun ()
#12 0x0001ea9b in UIApplicationMain ()
#13 0x00002a58 in main (argc=1, argv=0xbfffed50) at /Users/MikeW/Desktop/ARC test proj/lib1.0Test/lib1/main.m:16
#14 0x000029b5 in start ()
感谢您提供的任何帮助。
由于
麦克
答案 0 :(得分:0)
这与ARC没有任何关系。
如果它报告它没有响应选择器,那么它不响应。想出来的。很可能你在选择器中有一个拼写错误(例如,它们区分大小写)。或者代表实际上并不是你认为的那样。
顺便说一句,我会从您的选择器中移除空格,例如即使编译器喜欢它,@selector(apiOperation: didCompleteWithResult:)
也会出错。
答案 1 :(得分:0)
正如大卫所说,这是代表被释放的问题。
通过简单地使属性“(强)”而不是unsafe_unretained或__weak来解决问题。
@property (strong) id<KSAPIOperationDelegate,NSObject> delegate;