简体   繁体   中英

Works on iOS Simulator but not on iOS device?

EDIT: It works but it takes amazingly long to complete. Is this normal, or is there a way to optimize it?

Thanks

I am using DDUnitConverter in my project to convert currencies.

Everything works perfectly fine on the iOS Simulator but hangs when I try to convert the currencies on my iOS Device (iPhone 4 iOSv5.1). I looked around to find a fix to this issue but could not find anything. Here is the code that I use to exchange the currencies. The code within the DDUnitConverter is available here: https://github.com/davedelong/DDUnitConverter/downloads

if ([Number.text isEqualToString:@""] || [picklable.text isEqualToString:@"no selection"] || [picklable2.text isEqualToString:@"no selection"]) {
    return;
}

if ([Number.text isEqualToString:@"0"]) {
    Result.text = @"0";
    return;
}

int fromType;
int toType;

fromType = [list indexOfObject:picklable.text];
toType = [list indexOfObject:picklable2.text];

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * from = [[f numberFromString:Number.text] retain];
[f release];

NSNumber *to = [[[DDUnitConverter currencyUnitConverter] convertNumber:from fromUnit:fromType toUnit:toType] retain];
float toto = [to floatValue];
Result.text = [NSString stringWithFormat:@"%.4f %@", toto, picklable2.text];

if ((toto == 0 || toto == [Number.text floatValue]) && picklable.text != picklable2.text ) {
    Result.text = @"No Internet Connection or Previous Data";
}

[from release];
[to release];

[Result flashScrollIndicators];

Hopefully someone can help me out, Thanks

Anything taking amazingly long to do should be dispatched. Like this:

dispatch_async(dispatch_get_global_queue(), ^(void) {
    [self doReallyAmazinglyComplicatedProcessing];
});

Your code seems OK to me, but you are using DDUnitConverter. I've never used it, but I suppose it needs internet connection to load data from the internet. If the server take long time to answer, your app could hang on connection.

You can try to connect to the server asynchronously using dispatch_async , this lets your app download data in background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM