离开uiviewcontrolelr后,Dropbox上传会停止

时间:2015-05-16 10:52:49

标签: ios iphone dropbox

我在Dropbox上传时遇到问题。

当我留在我写过Dropbox上传代码的视图上时,它工作正常。但如果在上传过程中我离开视图并打开另一个屏幕,则上传停止或取消。

我正在使用以下代码

- (void)didPressLink {
    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    self.restClient.delegate = self;
    if(![[DBSession sharedSession] isLinked]) {
        [[DBSession sharedSession] linkFromController:self];
    }
}

-(void)saveFileOnDropBox{
    [self didPressLink];
    NSString *filename = [soundOneNew lastPathComponent];
    NSString *destDir = @"/";
    [[self restClient] uploadFile:filename toPath:destDir    withParentRev:nil fromPath:soundOneNew];
}

-(void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath
         from:(NSString*)srcPath metadata:(DBMetadata*)metadata {

     NSLog(@"File uploaded successfully to path: %@ %@", metadata.path, destPath);

     [[self restClient] loadSharableLinkForFile: [NSString stringWithFormat:@"/%@",metadata.path]];
}

- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error {
    NSLog(@"File upload failed with error - %@", error);
}

 - (void)restClient:(DBRestClient*)restClient loadedSharableLink:(NSString*)link
       forFile:(NSString*)path
{
   NSLog(@"Sharable link %@",link);
   NSLog(@"File Path %@ ",path);

}


-(void) restClient:(DBRestClient *)client uploadProgress:(CGFloat)progress forFile:(NSString *)destPath from:(NSString *)srcPath {
   static NSDate* date = nil;
   static double oldUploadedFileSize = 0;
   if (!date) {
       date = [NSDate date] ;
   } else {
        NSTimeInterval sec = -[date timeIntervalSinceNow];
        date = [NSDate date] ;
       NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:srcPath error:nil];
       double uploadedFileSize = (double)[fileAttributes fileSize] * progress;
       if (sec) {
           NSLog(@"speed approx. %.2f KB/s", (uploadedFileSize - oldUploadedFileSize )/1024.0 / sec );
       }
       oldUploadedFileSize = uploadedFileSize;
       self.uploadAudioProgress.progress = progress;
    }
}

即使我离开视图,如何继续上传过程

1 个答案:

答案 0 :(得分:2)

你打电话

self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];

创建一个新的REST客户端然后

[[self restClient] uploadFile:filename toPath:destDir withParentRev:nil fromPath:soundOneNew];

进行上传。因此,此客户端由视图控制器拥有,并在视图控制器被销毁时被销毁 - 即当您离开视图控制器时。

你想要一些其他的类和它的一个实例(可能是一个单独的或一个由app委托拥有的实例并传递给需要它的实例),它拥有所有创建的REST客户端并保留它们直到它们完成。< / p>