如何允许对象在回调时释放? [英] How to allow the object to be released on callback?
问题描述
我有一个写在目标C的类,通过连接到网络做一些工作。这个类以异步方式工作,用户给出一个委托,一旦操作完成(如果成功或错误,如果失败,响应)。
现在我想支持一个用例,当用户将释放我的对象时,类调用该委托。这是安静的自然,因为我的类的实例不可重复使用。目前,当我的回调从回调返回,它试图继续从它离开只发现它已被释放。不用说,应用程序崩溃了。
我知道这可以做,因为 NSURLConnection
。可以在回调 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
中释放连接对象。
这里是代码,代码如下:理解更好:
@class MyRequest;
@protocol MyDelegate
- (void)completedRequest:(MyRequest *)request;
@end
@interface MyRequest:NSObject {
id< MyDelegate> myDelegate;
//变量...
}
- (id)init;
- (id)initWithUrl:(NSString *)url delegate:(id< MyDelegate>)delegate;
- (void)dealloc;
- (void)fire;
@property(nonatomic,readonly)bool fired;
@property(nonatomic,readonly)NSInteger resultCode;
@property(nonatomic,readonly)NSString * errorMessage;
@end
用户创建 MyRequest
并发送 fire
消息。 MyRequest
内部使用 NSURLConnection
来完成这项工作。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@In connectionDidFinishLoading ...);
resultCode = RESULT_SUCCESS;
[urlConnection release];
[urlRequest release];
if(myDelegate!= nil)
[myDelegate completedRequest:self];
}
c> [[self retain] autorelease] ,然后调用委托回调来延长对象的生命周期。
另一种方法是使用 performSelectorOnMainThread
方法 NSObject
。 performSelector ...
机械将保留您的对象,调用方法,然后释放它。如果你使performSelector方法等待直到完成,那么你会有同样的问题,你现在,因为你会在该调用释放。如果你不等到它完成,那么你不应该有一个内存问题,但然后你不能做任何事后,它完成。如果这是可以接受的,那么你应该很好。
I have a class written in objective C which does some work by connecting to the network. This class does this work in async manner and user gives a delegate which is called once the operation is finished (with response if success or error if failure). The delegate is called when there is no more work to do and object can be released.
Now I want to support a use-case when user will release the object of my class in the call to that delegate. This is quiet natural since instance of my class are not reusable. Currently when my call returns from callback, it tries to continue from where it left only to find that it has been released. Needless to say, application crashes at that point.
I know it can be done because NSURLConnection
class does just that. It is OK to release the connection object in the callback - (void)connectionDidFinishLoading:(NSURLConnection *)connection
. How can I accomplish the same?
EDIT:
Here is code to understand it better:
@class MyRequest;
@protocol MyDelegate
- (void)completedRequest:(MyRequest*)request;
@end
@interface MyRequest : NSObject {
id<MyDelegate> myDelegate;
//variables...
}
- (id)init;
- (id)initWithUrl:(NSString*)url delegate:(id<MyDelegate>)delegate;
- (void)dealloc;
- (void)fire;
@property (nonatomic, readonly) bool fired;
@property (nonatomic, readonly) NSInteger resultCode;
@property (nonatomic, readonly) NSString *errorMessage;
@end
User creates the object of MyRequest
and sends fire
message. MyRequest
internally uses NSURLConnection
to do the work. When the response is received, I call the user given delegate.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"In connectionDidFinishLoading...");
resultCode = RESULT_SUCCESS;
[urlConnection release];
[urlRequest release];
if (myDelegate != nil)
[myDelegate completedRequest:self];
}
Just add [[self retain] autorelease]
before you invoke the delegate callback to extend your object's lifetime. This will cause it to be released later so you can finish up.
Another approach is to use performSelectorOnMainThread
method of NSObject
. performSelector...
machinery will retain your object, call the method, and then release it afterwards. If you make the performSelector method wait until finished, then you'll have the same problem you do now, since you'll get deallocated during that call. If you don't wait until it is finished, then you shouldn't have a memory issue, but then you can't do anything after it's done. If that's acceptable, then you should be fine.
这篇关于如何允许对象在回调时释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!