您的位置:首页 > 编程语言 > PHP开发

Delegate无法正确工作(有关Ftp控制)

2014-04-26 22:48 435 查看
最近研究Ftp上传、下载、创建、删除

因为NSStream没有提供删除的接口,要用CFURLDestroyResource删除,所以就用了国外人写的东西BlackRaccoon。里面的调用机制,使用委托协议等方法用的很好,值得学习。将其中的主要类的内容大概说下:

BRRequest类:主要存储空间及用户数据,BRRequestDelegate协议的定义

BRStreamInfo类:流的读、写

四个ftp功能类:上传、下载、创建、删除,继承于BRRequest类

在使用中发现问题:

在下载文件中发现,当数据流已经读下来了,但到BRStreamInfo类的(问题和原因都写在下面了)

- (void)streamComplete:(BRRequest *)request

{

writeWLLog(@"BRStreamInfo.streamComplete");

//下面这句话在这里程序里总是失败

//request.delegate无法实现委托

//原因找到了!因为BRRequest.h里@property (strong) id <BRRequestDelegate> delegate;

//不能用weak,要用strong

[request.delegate requestCompleted: request];

[request.streamInfo close: request];

}

解决方法:

在BRRequest.h中修改:

@property (strong) id <BRRequestDelegate> delegate;这里的strong是非常重要的一个东东,原来是

//weak,但会造成streaminfo里的streamComplete函数里的

//[request.delegate requestCompleted: request];不工作,原因就是在指针委托被摧毁了(释放)

感谢外国友人,下面是他们的原文!
https://github.com/lloydsargent/BlackRaccoon/issues/41
I've implemented an FTPHelper class, that does the FTP communication for me using BlackRaccoon, however, all the delegate/required methods are not called.

I'm trying to download a database file from my FTP server, it's not storing though, it did download it, since the data isn't communicated back requestCompleted.

I'm sure that my FTP server is working, and that the data is being transferred because I placed print statements in BRStreamInfo.m. In my understanding, when the stream is complete it should call requestCompleted according to the following snippet
- (void)streamComplete:(BRRequest *)request
{
NSLog(@"Stream Complete");
[request.delegate requestCompleted: request];
NSLog(@"Data tranferred back to FTP helper requestCompleted");
[request.streamInfo close: request];
}


This is my FTP helper method, FTP request information are stored as constants:

I found out what the problem is, it turns out that the pointer delegate is destroyed during the download process. I changed it's property from (weak) to (strong) and now it works.

In BRRequest.h:
@property (weak) id <BRRequestDelegate> delegate;
//Change the above to the below
@property (strong) id <BRRequestDelegate> delegate;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐