问题
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