适用于iOS的DropBox sync api在下载图像时不会调用观察者

时间:2014-03-14 02:07:20

标签: ios objective-c dropbox-api

您好我正在使用DBX sync api同步我的应用数据并从DBX下载图像。在我使用核心api下载图像之前,它工作正常。但核心和同步apis不会一起工作。所以我也切换到同步api下载文件,但现在下载图像,其中没有调用进度观察器。这是我的观察员代码。

DBFile *orignalImg = [[DBFilesystem sharedFilesystem]openFile:imgPath error:nil];
                NSLog(@" -----> %@, %i , %@", orignalImg,orignalImg.status.state,  imgInfo.imgPath);

                __weak DBFile *oFile = orignalImg;

                [orignalImg addObserver:self block:^(void)
                 {
                    if (fileStatus.cached) // if image downloaded
                    {
                       //save image
                    }
                    else if (fileStatus.state == DBFileStateDownloading) // show progress bar
                   {
                   }


                 }];

我试过这个代码DBFile是从openfile方法返回的,但是没有调用observer。

2 个答案:

答案 0 :(得分:1)

假设您正在使用ARC,一旦局部变量orignalImg超出范围,它将被解除分配,阻止它做任何事情。

只要您希望观察它,您就需要维护对DBFile实例的引用。将其作为实例变量是一种选择。

答案 1 :(得分:-1)

我遇到了同样的问题,当我从云端获取文件时,观察者没有工作。在查看SDK中的示例并找到解决方案之后。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^() {
        DBPath *db_path = [[DBPath root] childPath:<YOUR_FILE_NAME>];
        DBError *err;
        DBFileInfo *file_info = [[DBFilesystem sharedFilesystem] fileInfoForPath:db_path error:&err];
        if (file_info) {
               dispatch_async(dispatch_get_main_queue(), ^() {
                    NSLog(@"file existed %@", file_info);
                    DBError *err2;
                    DBFile *db_file = [[DBFilesystem sharedFilesystem] openFile:db_path error:&err2];
                    __weak id weakFile = db_file;                           
                    if (![[db_file status]cached]) {
                       [db_file addObserver:self block:^{
                            //....get progress, status and do your logic here
                       }];
                });
         }
});