Reputation: 197
I have a weird issue. I have a class that calls a method in another class on a background thread, but it fetches data twice on Thread 2 and then once it finishes puts it to thread 4 for example. Does anyone know why?
Class that calls the method:
-(void)awakeFromNib
{
NSString *swedbankRate;
CurrencyViewHelper *currencyRates = [[CurrencyViewHelper alloc]init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
swedbankRate = [currencyRates getSwedbankRates];
NSLog(@"%@", swedbankRate);
});
So the part from the CurrencyViewHelper is:
-(NSString *)getSwedbankRates {
// Set to empty String in case there is some issue
swedbankRate = @"";
// Get Data from Swedbank
NSString *swedbankURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://www.swedbank.ee/private/d2d/payments2/rates/currency?language=EST"]
encoding:NSASCIIStringEncoding
error:nil];
NSArray *swedbankArray = [swedbankURL componentsSeparatedByString:@"<tr>"];
NSString *swedbankString = swedbankArray[18];
swedbankArray = [swedbankString componentsSeparatedByString:@"<tr>"];
NSString *swedbankDirty = swedbankArray[0];
// Check if the fetched data corresponds to RUB data
if ([swedbankDirty containsString:@"RUB"]) {
swedbankDirty = [swedbankDirty stringByReplacingOccurrencesOfString:@"<td class=\"numeric-cell\">"
withString:@""];
swedbankDirty = [swedbankDirty stringByReplacingOccurrencesOfString:@"\n"
withString:@""];
swedbankDirty = [swedbankDirty stringByReplacingOccurrencesOfString:@" "
withString:@""];
NSArray *swedbankCleanArray = [swedbankDirty componentsSeparatedByString:@"</td>"];
swedbankDirty = swedbankCleanArray[4];
// Set result to value fetched from internet
swedbankRate = [NSString stringWithFormat:@"%.2f", [swedbankDirty floatValue]];
}
And result is
2015-12-07 14:26:29.635 Photography[60338:18355297] 71.14
2015-12-07 14:26:29.684 Photography[60338:18355293] 71.14
I cant put my finger on it why does it get executed twice?
What the method is supposed to do is scan the HTML code of a webpage and get just one float number from the whole content for me to use later. Ye the code isnt perfect, still learning. Any ideas welcome.
Upvotes: 0
Views: 114
Reputation: 52632
awakeFromNib is usually not a good place for that kind of thing. You should use awakeFromNib to do things that you would have wanted to do in Interface Builder, but for some reason couldn't - so just to fix problems that arise when you load a nib file.
For example, doing things in awakeFromNib gets you into trouble if you use the same class in a different context, and if you build a user interface in code instead of using a nib file, and your awakeFromNib doesn't get called at all anymore.
Upvotes: 1
Reputation: 197
You are both correct. Wow, what a silly mistake to make. Indeed another class was also calling awakeFromNib to do its stuff there too.
I put those calls in the viewDidLoad method and it is working as it needs now! Great! What a great way to learn this multithreading stuff! :)
Upvotes: 0
Reputation: 41
Check if awakeFromNib
is called twice in your app... (see this question)
Upvotes: 0