Team
i have to find the constant bandwith when i am login and usuing application till i am not log out...
i have try this code but it gives only one time speed i want constant speed track of bandwith ..
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.downloadSpeedMeasure.active = YES;
[operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
}];
// to avoid a retain cycle one has to pass a weak reference to operation into the progress block.
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = ((float)totalBytesRead) / totalBytesExpectedToRead;
NSLog(#"%f",progress);
double speedInBytesPerSecond = operation.downloadSpeedMeasure.speed;
NSString *humanReadableSpeed = operation.downloadSpeedMeasure.humanReadableSpeed;
double humanReadableSpeed1 = operation.downloadSpeedMeasure.speed;
// NSTimeInterval remainingTimeInSeconds = [operation.downloadSpeedMeasure remainingTimeOfTotalSize:totalBytesExpectedToRead numberOfCompletedBytes:totalBytesRead];
// NSString *humanReadableRemaingTime = [operation.downloadSpeedMeasure humanReadableRemainingTimeOfTotalSize:totalBytesExpectedToRead numberOfCompletedBytes:totalBytesRead];
NSLog(#"humanReadableSpeed : %#",humanReadableSpeed);
NSLog(#"humanReadableSpeed : %f",humanReadableSpeed1);
//NSLog(#"speedInBytesPerSecond : %f",speedInBytesPerSecond);
}];
[operation start];
can any one help me to find out how to get constant bandwith using any third party libs,functuion,demo????
Thanx in advance...
i'm developing an video download application but there is a problem. User did enter background (while downloading), after several minutes ago, when user did call back application from background, the downloading file get a problem. For example, the file must resume from (for example) 34%. it resumes but downloading completing on 134% !? in other words, i get %100 of the file but, on %134. is anybody have an idea? Sorry for my bad english.
Below, there are my codes i used, got from AFNetworking ;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Successfully downloaded file to %#", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
To see percentage of the file, i use following codes;
[[MUtility sharedObject].operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(#"Download = %f", (float)totalBytesRead / totalBytesExpectedToRead *100);
}];
I'm writting a client/server application that needs to send some XML to a server.
NSMutableArray *operations = [NSMutableArray array];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
[operation1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(#"Progress 1 = %f",progress);
}];
[operations addObject:operation1];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
[operation2 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(#"Progress 2 = %f",progress*100);
}];
[operations addObject:operation2];
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:#[operation1, operation2] waitUntilFinished:NO];
What I want to do now is to handle if the transmission fails and have a queue so it retries to send it.
What's the best way to achieve this with the AFNetworking library ?
First up it would not be very wise to just retry failed operations again. Depending on what was the source of the error, you risk severe side effects like duplicate submissions.
You're already using AFHTTPRequestOperation, so the easiest solution would be to call
setCompletionBlockWithSuccess:failure: and handle errors in the "failure"-block. After all you may also want to use the "success"-block for when the download successfully finished.
One last detail about the code you provided: You're creating the NSArray *operations in line 1 - yet you're not using it for anything since you create a new array of the operations in the last line. So either you left something out or you should simplify that.
I am using AFNetworking to download files that my app uses for a sync solution. At certain times, the app downloads a series of files as a batch unit. Following this example, I run the batch like this:
NSURL *baseURL = <NSURL with the base of my server>;
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
// as per: https://stackoverflow.com/a/19883392/353137
dispatch_group_t group = dispatch_group_create();
for (NSDictionary *changeSet in changeSets) {
dispatch_group_enter(group);
AFHTTPRequestOperation *operation =
[manager
POST:#"download"
parameters: <my download parameters>
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// handle download success...
// ...
dispatch_group_leave(group);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle failure...
// ...
dispatch_group_leave(group);
}];
[operation start];
}
// Here we wait for all the requests to finish
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// run code when all files are downloaded
});
This works well for the batch downloads. However, I want to display to the user an MBProgressHUD which shows them how the downloads are coming along.
AFNetworking provides a callback method
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
}];
... which lets you update a progress meter pretty easily, simply by setting the progress to totalBytesRead / totalBytesExpectedToRead. But when you have multiple downloads going simultaneously that is hard to keep track of on a total basis.
I have considered having an NSMutableDictionary with a key for each HTTP operation, with this general format:
NSMutableArray *downloadProgress = [NSMutableArray arrayWithArray:#{
#"DownloadID1" : #{ #"totalBytesRead" : #0, #"totalBytesExpected" : #100000},
#"DownloadID2" : #{ #"totalBytesRead" : #0, #"totalBytesExpected" : #200000}
}];
As each operation's download progresses, I can update the totalBytesRead for that specific operation in the central NSMutableDictionary -- and then total up all the totalBytesRead and totalBytesExpected' to come up with the total for the whole batched operation. However, AFNetworking's progress callback methoddownloadProgressBlock, defined as^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){}does not include the specific operation as a callback block variable (as opposed to thesuccessandfailure` callbacks, which do contain the specific operation as a variable, making it accessible). Which makes it impossible, as far as I can tell, to determine which operation specifically is making the callback.
Any suggestions on how to track the progress of multipole simultaneous downloads using AFNetworking?
If your block is inlined, you can access the operation directly but the compiler might warn you of the circular referencing. You can work around by declaring a weak reference and use it inside the block:
__weak AFHTTPRequestOperation weakOp = operation;
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSURL* url = weakOp.request.URL; // sender operation's URL
}];
Actually, you can access anything inside the block, but you need to understand block to go for that. In general, any variable referred in the block is copied at the time the block created i.e. the time that the line got executed. It means my weakOp in the block will refer to the value of the weakOp variable when the setDownloadProgressBlock line got executed. You can think it like what would each variable you refer in the block would be if your block got executed immediately.
Blocks are made just to makes these things easyer ;-)
HERE you can find an example project. Simply push the + button and insert the direct URL for a file to download. There is no error checking and no URL redirection so insert only direct URLs.
For the relevant part look in these methods of the DownloadViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Here the explanation:
When you pass a variable to a block, the block makes a copy of the variables passed from outside.
Because our variable is simply an object pointers (a memory address), the pointer is copied inside the block, and since the default storage is __strong the reference is maintained until the block is destroyed.
It means that you can pass to the block a direct reference to your progress view (I use an UIProgressView since I've never used MBProgressHUD):
UIProgressView *progressView = // create a reference to the view for this specific operation
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
progressView.progress = // do calculation to update the view. If the callback is on a background thread, remember to add a dispatch on the main queue
}];
Doing this, every operation will have a reference to its own progressView. The reference is conserved and the progress view updated until the progress block exists.
You must be downloading some zip file , video file etc.
Make a model of that file to download containing fields like
(id, url, image , type , etc...)
Create a NSOperationQueue
and set maxConcurrentOperationCount according to your requirement
make public method. (in a singleton class)
- (void)downloadfileModel:(ModelClass *)model {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.zip",model.iD]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.url]];
operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setUserInfo:[NSDictionary dictionaryWithObject:model forKey:#"model"]];
////// Saving model into operation dictionary
[operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
////// Sending Notification
////// Try to send notification only on significant download
totalBytesRead = ((totalBytesRead *100)/totalBytesExpectedToReadForFile);
[[NSNotificationCenter defaultCenter] postNotificationName:DOWNLOAD_PROGRESS object:model userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"%lld",totalBytesRead] forKey:#"progress"]];
/// Sending progress notification with model object of operation
}
}];
[[self downloadQueue] addOperation:operation]; // adding operation to queue
}
Invoke this method for multiple times with different models for multiple downloads.
Observe that notification on controller
where you show the download progress. (possibly tableView Controller).
Show all downloading operations list in this class
For showing progress Observe the Notification and fetch the model object from notification and get the file id from notification and Find that id in your Table View and Update that particular cell with the progress.
when starting the operations, you could safe each operation, downloadID and the values for totalbytesRead and totalBytesExpected together in a NSDictionary and all dicts to your downloadProgressArray.
Then when the callback methods is invoked, loop through your array and compare the calling operation with the operation in each dict. this way you should be able to identify the operation.
I just did something very similar (uploaded a bunch of files instead of downloading)
Here's an easy way to solve it.
Lets say you are downloading maximum of 10 files in one batch.
__block int random=-1;
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
if (random == -1) // This chunk of code just makes sure your random number between 0 to 10 is not repetitive
{
random = arc4random() % 10;
if(![[[self myArray]objectAtIndex:random]isEqualToString:#"0"])
{
while (![[[self myArray]objectAtIndex:random]isEqualToString:#"0"])
{
random = arc4random() % 10;
}
}
[DataManager sharedDataManager].total += totalBytesExpectedToWrite;
}
[[self myArray] replaceObjectAtIndex:random withObject:[NSString stringWithFormat:#"%lu",(unsigned long)totalBytesWritten]];
Then you calculate it like this:
NSNumber * sum = [[self myArray] valueForKeyPath:#"#sum.self"];
float percentDone;
percentDone = ((float)((int)[sum floatValue]) / (float)((int)[DataManager sharedDataManager].total));
[self array] will look like this:
array: (
0,
444840, // <-- will keep increasing until download is finished
0,
0,
0,
442144, // <-- will keep increasing until download is finished
0,
0,
0,
451580 // <-- will keep increasing until download is finished
)
I am using AFDownloadRequestOperation. It was working fine, but is now suddenly giving this error every time I run it (I am downloading from the same URL every time I run it and I do clear the Documents and Temp folders in AppDelegate).
Initiate a download. (Before downloading, I do manually check that there is nothing in Temp or Documents folder, i.e. where I store the files once download is done).
The download gets over in 1-2 seconds, code moves to success block, the debug console shows a 206 status code, a file magically gets placed in Documents folder out of nowhere and has the exact file size (it used to get downloaded properly until I started running into this issue). The file is corrupt and does not open.
Here is the code snippet -
download_operation_progress_block = ^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
float progress = ((float)totalBytesReadForFile) / totalBytesExpectedToReadForFile;
dataFetcherObject.downloadProgressDetails.downloadPercentage = [NSNumber numberWithFloat:floorf(progress*100)];
};
download_operation_success_block = ^(AFHTTPRequestOperation *operation, id responseObject) {
dataFetcherObject.downloadProgressDetails.isDownloadDone = [NSNumber numberWithBool:YES];
dataFetcherObject.isDataFetched = #"Yes";
};
download_operation_failure_block = ^(AFHTTPRequestOperation *operation, NSError *error) {
[dataFetcherObject.downloadProgressDetails.downloadOperation cancel];
dataFetcherObject.isDataFetched = #"Error";
};
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:contentDownloadRequest
targetPath:path
shouldResume:YES];
[operation setCompletionBlockWithSuccess:download_operation_success_block
failure:download_operation_failure_block];
[operation setProgressiveDownloadProgressBlock:download_operation_progress_block];
// operation.deleteTempFileOnCancel = TRUE;
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:operation];
dataFetcherObject.downloadProgressDetails.downloadOperation = operation;
What could be the reason for this.