I am using NSURLSession to Download multiple files and update in database. download s happening correct for all files and database is updating properly. When i delete item from local as well database, it is deleting properly.
But, when i download new file from list, which contains part_id and Filename and click on download, at that time the part_id is not of current file. it is of last file part_id, which i deleted just before new download.
Original filename and part id before deleting any file locally and Database
-
FileName - Body Part ID - 2001
-
FileName - Break Part ID - 2501
-
FileName - Ignition Part ID - 6501
I selected- Automission Transmission file, its Part ID is 1011 but in Database it is updating with Part ID of Ignition System, which is 6501
Video Url is updating correct of Automission Transmission
Code for removing file -
- (void)removeVideoFile:(NSString *)fileName
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *VideoPath = [documentsDir stringByAppendingPathComponent:fileName];
NSLog(@"Database Path : %@",VideoPath);
NSLog(@"file name : %@", fileName);
NSError *error;
BOOL success = [fileManager removeItemAtPath:VideoPath error:&error];
if (success)
{
NSLog(@"\n Part ID :%@",part_id);
if ([self DBUpdateVideoURLStatus:part_id andFileName:NULL])
{
[self.tempDownloadbtn setSelected:NO];
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"0.0"] waitUntilDone:NO];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Success", @"Success message") message:NSLocalizedString(@"Video deleted successfully", @"Success message") delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok action") otherButtonTitles:nil];
[removeSuccessFulAlert show];
[removeSuccessFulAlert release];
NSLog(@"\n\n Part ID :%@",self.part_id);
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error", @"Alert Title") message:NSLocalizedString(@"", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok action") otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}
//DataBase Video Update
-(BOOL)DBUpdateVideoURLStatus : (NSString *)partid andFileName:(NSString *) fileName
{
NSLog(@"Partid : %@",partid);
NSLog(@"fileName : %@",fileName);
NSString *databasePath;
sqlite3 *database;
NSString *databaseName;
NSString *documentsDir;
NSArray *documentPaths;
databaseName = @"VirtualVehicle1.sqlite";
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
const char *sqlStatement;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
sqlStatement = "UPDATE tblAnimations SET lcl_video_url=? WHERE part_id=?";
NSLog(@"querry string = %@",[NSString stringWithFormat:@"%s",sqlStatement]);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_text( compiledStatement, 1,[fileName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text( compiledStatement, 2,[partid UTF8String], -1, SQLITE_TRANSIENT);
if(sqlite3_step(compiledStatement) == SQLITE_DONE)
{
sqlite3_reset(compiledStatement);
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
return TRUE;
}
else
{
NSLog(@"Update failed: %s", sqlite3_errmsg(database));
}
}else{
NSLog(@"Error occured while updated video url to blank: %s", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return FALSE;
}
// NSURLSession Delegate for Downloading
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double Progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
NSLog(@"Progressview progress : %f",Progress);
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown)
NSLog(@"Unknown transfer size");
else
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"%f",Progress] waitUntilDone:NO];
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destinationFilename = downloadTask.originalRequest.URL.lastPathComponent;
NSLog(@"Part id : %@",self.part_id);
NSURL *destinationURL = [self.docDirectoryURL URLByAppendingPathComponent:destinationFilename];
if ([fileManager fileExistsAtPath:[destinationURL path]])
{
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location
toURL:destinationURL
error:&error];
if (success)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Download completed!");
NSLog(@"destination file name : %@!",destinationFilename);
NSLog(@"Part id of file name : %@!",self.part_id);
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"1.0"] waitUntilDone:NO];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *filenameValue=[NSString stringWithFormat:@"%@",destinationFilename];
NSString *hideExtension=[filenameValue substringToIndex:filenameValue.length-4];
NSLog(@"filenameValue == %@", filenameValue);
[self DBUpdateVideoURLStatus:self.part_id andFileName:filenameValue];
[self DBInsertDownloads:self.part_id andFileName:filenameValue];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Success", @"Success message") message:[NSString localizedStringWithFormat:@"%@ downloaded successfully!",hideExtension] delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok action") otherButtonTitles:nil];
alert.tag = 1;
[alert show];
[alert release];
[self.session invalidateAndCancel];
}];
}
else
{
NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]);
}
}
Aucun commentaire:
Enregistrer un commentaire