1

I'm using the NSNotification centre to detect changes of currency so I can update all the other classes. When a currency change occurs, all the other classes and views get updated, however when there is no currency change, and if you press the back button to go back to the home page the view loads on top of the already existing view.

Code for NSNotification center

  if ([overviewModel.currency isEqual:@"GBP"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                                    object:self];
            } else {

                [[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                                    object:self];
            }

Code for handling updated data in homepage:

  for (UIView *b in self.view.subviews) {
    [b removeFromSuperview];
}


self.build = [[ApiRequestBuild alloc]initWithVersionKey:kAPI_VERSION_KEY requestType:kAPI_REQUEST_TYPE data:@""];
[self.build setQueryWithSection:@"homepage" value:@"" parameter:@[]];

self.request = [[ApiRequest alloc]init];
self.request.delegate = self;
[self.request sendRequestWithParams:[self.build buildConfig] toUrl:kAPI_URL_STRING];

I know why this is happening, the request gets sent again so the page loads on top of the already existing page, what I don't understand is why doesn't the remove from subview code get rid of the of the view and how would I be able to fix this? thanks

Pooja Srivastava
  • 721
  • 1
  • 6
  • 19
  • have you tried this ? - https://stackoverflow.com/a/11889296/3548469 – Devang Tandel Aug 10 '17 at 11:03
  • @Dev_Tandel yes, still the same issue. I'm gonna create a global variable and set it to nil ,then add a value to it only if the currency is changed so that would be the only time the homepage would get updated. –  Aug 10 '17 at 11:10
  • but why you want to remove subviews ? you just need to update all when currency is changed – Devang Tandel Aug 10 '17 at 11:13
  • @Dev_Tandel to update the homepage, I send another request to get the homepage again and if I don't remove the subview, it adds the view on top of the existing view. –  Aug 10 '17 at 11:21
  • sorry @Taha Amini - i am really not getting who you wish to achieve this and get homePage.. you are developing native app right? – Devang Tandel Aug 10 '17 at 11:26
  • @Dev_Tandel okay let's say for example I have a label in the home page that displays "£", and if I click on that label, it will take me to a different class, now in the new class I can change the label text from "£" to "€", and I send a request to the server to change the currency, now if I go back to the homepage, I need to send a request to get the correct value of the label, otherwise the label's text will still stay the same. hope this helped. –  Aug 10 '17 at 11:34
  • If you have instance of View you want to remove. Directly remove it from super view. – Aadil Ali Aug 10 '17 at 11:41
  • have you created all lable programatically ? – Devang Tandel Aug 10 '17 at 11:52
  • if not why dont you just reset the values in lable, if would be simple – Devang Tandel Aug 10 '17 at 11:53
  • @Dev_Tandel how would I just rest the values in labels ? do I need to create an instance of them then just call it whenever the currency gets updated? –  Aug 10 '17 at 12:15
  • yes, you can simply do with drag it to your .h file – Devang Tandel Aug 10 '17 at 12:16
  • @Dev_Tandel I have no idea why, but it doesn't update it. thanks for the suggestion thu, i'm gonna figure out why it's not updating it –  Aug 10 '17 at 13:27
  • did you gave outlets to your label ? – Devang Tandel Aug 10 '17 at 13:28
  • self.productCoreDetailView.priceLabel.text = [NSString stringWithFormat:@"%@%@",currencySign, overviewModel.price_vat]; thats the code. –  Aug 10 '17 at 13:35
  • @Dev_Tandel i've logged it and it returns nul, i think i know why –  Aug 10 '17 at 13:38
  • Good..try it out...let us know if solved or you need further help. – Devang Tandel Aug 10 '17 at 13:39

1 Answers1

0

The removeFromSuperview won't work if it's being called from another thread (than main thread). Your notification will be received on the same thread it was fired from. I'll wager that you're listening to a model change event (regarding your currency state) on another thread.

Try dispatching to main queue before walking your copy of subviews to remove them all.

Smartcat
  • 2,834
  • 1
  • 13
  • 25