无法处理数据库和文档目录中数据存储的后台进程?
Unable to handle background process for data store in database and document directory?
我正在从照片库中选择多个图像和视频,并将资产路径存储到数据库中,并将原始资产存储到应用程序文档目录中。对于这个过程,直到它完成我显示进度条状态。
但问题是当我开始保存进程进度条状态保持不变,它会在保存操作完成后增加进程状态。
我正在使用 SVProgressHUD 显示进度条。
我在选择资产后所做的代码。
static float progress = 0.0f;
progress = 0.0f;
[SVProgressHUD showProgress:0 status:@"Loading"];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(SaveAssetToSQL) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(load_Album_Data) userInfo:nil repeats:NO];
我在数据一一保存的时候调用了IncreaseProgress方法。
保存资源的方法。
-(void)SaveAssetToSQL{
for (int i=0; i<TotalAssets; i++) {
[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.3f];
//further process for save
}
}
IncreaseProcess 方法
- (void)increaseProgress {
progress+=0.1f;
[SVProgressHUD showProgress:progress status:[NSString stringWithFormat:@"%f",progress]];
if(progress < 1.0f)
[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.9f];
else
[self performSelector:@selector(dismiss) withObject:nil afterDelay:0.4f];
}
我必须显示进度条及其状态,直到资产存储过程完成。我还尝试使用 GCD 将该存储过程放在后台线程中,但无法显示进度状态。
请帮我解决这个问题。
请建议我一种处理此过程的方法。
关于我的问题,有任何方法可以在后台处理存储过程并在前台显示进度条状态。
帮我解决这个问题。
我建议您采用以下方法,使用 GCD
进行 long-运行 操作。
- 在不同的线程中执行文件保存部分。
- 计算每个文件复制后的完成百分比。
- 在主线程调用进度更新
它看起来应该类似于下面的代码。我只是为您托管框架代码。
dispatch_queue_t myQueue = dispatch_queue_create("MyQueue",NULL);
dispatch_async(myQueue, ^{
__block percentCompleted = 0;
NSArray *files = [self getFiles];//assume you get the file locations from this method.
for(NSString *filepath in files){//loop the files
//Perform copying file to new location
//Calculate how much percent has been completed
percentCompleted = <newValue>;
dispatch_async(dispatch_get_main_queue(), ^{
// Update progress-bar UI
});
}
});
我正在从照片库中选择多个图像和视频,并将资产路径存储到数据库中,并将原始资产存储到应用程序文档目录中。对于这个过程,直到它完成我显示进度条状态。 但问题是当我开始保存进程进度条状态保持不变,它会在保存操作完成后增加进程状态。 我正在使用 SVProgressHUD 显示进度条。 我在选择资产后所做的代码。
static float progress = 0.0f;
progress = 0.0f;
[SVProgressHUD showProgress:0 status:@"Loading"];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(SaveAssetToSQL) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(load_Album_Data) userInfo:nil repeats:NO];
我在数据一一保存的时候调用了IncreaseProgress方法。 保存资源的方法。
-(void)SaveAssetToSQL{
for (int i=0; i<TotalAssets; i++) {
[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.3f];
//further process for save
}
}
IncreaseProcess 方法
- (void)increaseProgress {
progress+=0.1f;
[SVProgressHUD showProgress:progress status:[NSString stringWithFormat:@"%f",progress]];
if(progress < 1.0f)
[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.9f];
else
[self performSelector:@selector(dismiss) withObject:nil afterDelay:0.4f];
}
我必须显示进度条及其状态,直到资产存储过程完成。我还尝试使用 GCD 将该存储过程放在后台线程中,但无法显示进度状态。 请帮我解决这个问题。 请建议我一种处理此过程的方法。 关于我的问题,有任何方法可以在后台处理存储过程并在前台显示进度条状态。 帮我解决这个问题。
我建议您采用以下方法,使用 GCD
进行 long-运行 操作。
- 在不同的线程中执行文件保存部分。
- 计算每个文件复制后的完成百分比。
- 在主线程调用进度更新
它看起来应该类似于下面的代码。我只是为您托管框架代码。
dispatch_queue_t myQueue = dispatch_queue_create("MyQueue",NULL);
dispatch_async(myQueue, ^{
__block percentCompleted = 0;
NSArray *files = [self getFiles];//assume you get the file locations from this method.
for(NSString *filepath in files){//loop the files
//Perform copying file to new location
//Calculate how much percent has been completed
percentCompleted = <newValue>;
dispatch_async(dispatch_get_main_queue(), ^{
// Update progress-bar UI
});
}
});