DropBox sync api for iOS not call observer when download images

和自甴很熟 提交于 2019-12-25 18:26:52

问题


Hi i am using DBX sync api to sync my app datas and download images from DBX. Before i used core api for download images it works fine. But core and sync apis wont work together. So i switched to sync api for download files also but now download images with progress observer not called. this is my observer code.

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
                   {
                   }


                 }];

I tried this code DBFile is returned from openfile method but observer is not called.


回答1:


Assuming you are using ARC, as soon as the local variable orignalImg goes out of scope it will be deallocated preventing it from doing anything.

You need to maintain a reference to the DBFile instance for as long as you wish to observe it. Making it an instance variable is one option.




回答2:


I have had the same problem, the observer did not work when I took a file from cloud. After looking at the examples in SDK and found solution.

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
                       }];
                });
         }
});


来源:https://stackoverflow.com/questions/22394820/dropbox-sync-api-for-ios-not-call-observer-when-download-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!