下载图像时,iOS的Dropbox Sync API不致电观察者



嗨,我正在使用dbx sync api同步我的应用数据并从dbx下载图像。在使用Core API下载图像之前,它可以正常工作。但是核心和同步API无法共同起作用。因此,我也切换到同步API以获取下载文件,但是现在下载了未调用Progress观察者的图像。这是我的观察者代码。

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方法返回的,但观察者未调用。

假设您正在使用ARC,一旦局部变量orignalImg不出现范围,它将被交易,以防止其做任何事情。

只要您希望观察它,就需要维持对DBFile实例的引用。使其成为实例变量是一个选项。

我也遇到了相同的问题,当我从云中获取文件时,观察者无效。在查看了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
                       }];
                });
         }
});

最新更新