我在保管箱上传时遇到问题。
当我停留在我编写了 Dropbox 上传代码的 View 上时,它工作正常。但是,如果在上传过程中我离开 View 并打开另一个屏幕,则上传将停止或取消。
我正在使用下面的代码
- (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;
}
}
即使我离开 View ,如何保持上传过程继续
最佳答案
你打电话
self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
然后创建一个新的 REST 客户端
[[self restClient] uploadFile:filename toPath:destDir withParentRev:nil fromPath:soundOneNew];
上传。因此,此客户端由 View Controller 拥有,并在 View Controller 被销毁时被销毁 - 即当您离开它时。
你想要一些其他类和它的一个实例(可能是一个单例或一个由应用程序委托(delegate)拥有并传递给需要它的实例),它拥有所有创建的 REST 客户端并保留它们直到它们完成。
关于ios - 离开 uiviewcontroller 时 Dropbox 上传停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274736/