Simple Use of NSTimers in Cocoa

simple

September 3, 2006

Cocoa

No Comment

[cocoa]
#pragma mark Timer for the progress indicator and status updates
– (void)destroyDelayTimer
{
[mDelayTimer invalidate];
[mDelayTimer release];
mDelayTimer = nil;
}

– (void)fireUploadTimer:(NSTimer*)timer
{
totalTransferTime += timerUpdateTime;
// workaround for longlong to float conversion problem
float transferSpeed = [[[NSString alloc] initWithFormat:@”%d”, [con transferSpeed]] floatValue];

if ([con numberOfTransfers] < 1) {
[self destroyDelayTimer];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:@”100.0″ forKey:@”progressNumber”]
:@”ConnectionStatusProgressIndicator”];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:@”Transfer finished…” forKey:@”text”]
:@”ConnectionStatusTextField”];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:@”true” forKey:@”progressCancel”]
:@”ConnectionStatusProgressIndicator”];
}
if(transferSpeed >= 1.0f) {
NSString *transferMessage = [[NSString alloc] initWithFormat:@”Uploading at %1.2f Kilobytes per second”, [[[UnitConvertor alloc] convertKiloForBase:[[NSNumber alloc] initWithFloat:transferSpeed]] doubleValue]];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:transferMessage forKey:@”text”]
:@”ConnectionStatusTextField”];
if (lastTransferSpeed >= 1.0f) {
float percentsTransfered;
percentsTransfered = roundf( ( (totalTransferTime * lastTransferSpeed) / fileSize ) * 100.0f );
if ( ( lastValueForPercentsTransfered < percentsTransfered ) && (percentsTransfered <= 100.0f) ) {
// NSLog(@”percentsTransfered: %1.2f”,percentsTransfered);
// set percents transfered
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:[[NSString alloc] initWithFormat:@”%1.2f”, percentsTransfered] forKey:@”progressNumber”]
:@”ConnectionStatusProgressIndicator”];
lastValueForPercentsTransfered = percentsTransfered;
}
}
lastTransferSpeed = transferSpeed;
}

}

– (void)fireDownloadTimer:(NSTimer*)timer
{
totalTransferTime += timerUpdateTime;
// workaround for longlong to float conversion problem
float transferSpeed = [[[NSString alloc] initWithFormat:@”%d”, [con transferSpeed]] floatValue];

if ([con numberOfTransfers] < 1) {
[self destroyDelayTimer];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:@”100.0″ forKey:@”progressNumber”]
:@”ConnectionStatusProgressIndicator”];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:@”Transfer finished…” forKey:@”text”]
:@”ConnectionStatusTextField”];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:@”true” forKey:@”progressCancel”]
:@”ConnectionStatusProgressIndicator”];
}
NSLog(@”Dl: %f”,transferSpeed);
if(transferSpeed >= 1.0f) {
NSString *transferMessage = [[NSString alloc] initWithFormat:@”Downloading at %1.2f Kilobytes per second”, [[[UnitConvertor alloc] convertKiloForBase:[[NSNumber alloc] initWithFloat:transferSpeed]] doubleValue]];
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:transferMessage forKey:@”text”]
:@”ConnectionStatusTextField”];
if (lastTransferSpeed >= 1.0f) {
float percentsTransfered;
percentsTransfered = roundf( ( (totalTransferTime * lastTransferSpeed) / fileSize ) * 100.0f );
if ( ( lastValueForPercentsTransfered < percentsTransfered ) && (percentsTransfered <= 100.0f) ) {
// NSLog(@”percentsTransfered: %1.2f”,percentsTransfered);
// set percents transfered
[self BroadcastToObjects:[NSDictionary dictionaryWithObject:[[NSString alloc] initWithFormat:@”%1.2f”, percentsTransfered] forKey:@”progressNumber”]
:@”ConnectionStatusProgressIndicator”];
lastValueForPercentsTransfered = percentsTransfered;
}
}
lastTransferSpeed = transferSpeed;
}

}
[/cocoa]

Related Posts

iPhone OS: Design a nice InApp Purchase View

To get up a nice InAppPurchase View for Appleā€™s AppStore, we first need some good looking buttons: http://code.google.com/p/iphonegradientbuttons/ http://undefinedvalue.com/2010/02/27/shiny-iphone-buttons-without-photoshop/ http://iphonedevelopment.blogspot.com/2010/05/improved-gradient-buttons.html http://stackoverflow.com/questions/422066/gradients-on-uiview-and-uilabels-on-iphone/ There are also some tweaks for the UITableView you might consider using: http://stackoverflow.com/questions/400965/how-to-customize-the-background-border-colors-of-a-grouped-table-view/ http://stackoverflow.com/questions/986392/programmatically-force-a-uiscrollview-to-stop-scrolling-for-sharing-a-table-view/ more information upcoming…

Read More

iPhone OS: Setup an additional Security Layer

As you might already know the iPhone is itself not that secure that people might think (news is german): http://www.heise.de/security/meldung/Luecke-in-Datenverschluesselung-des-iPhones-1007818.html How do you prevent intrusion of your customers data? As a really pragmatic approach, we can just set up additional encryption for our application data: http://stackoverflow.com/questions/2579453/nsdata-aes-class-encryption-decryption-in-cocoa http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html http://pastie.org/974094/ So if you use for example core […]

Read More

Leave a Reply