1

I have situation like this. Suppose VC A has some list of event which has like functionality When user click on any event they can redirect to it's details page where there is name of event organizer On click on of event organizer load it all event Now if i like one event of that organizer then it also reflect at VC A.

There are three ways I thoughts

1) I used NSNotificationCenter but it fire multiple time because same VC appear multiple time in cycle

2) Delegate chaining also not possible for this scenario

3) KVO also not compatible because in profile new api call happen

Currently i am using database for this but in ViewWillAppear there are lot's of code more management

Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • You can make use of objects, update the value of the object and reload in viewWillAppear.. But if you need persistence of data which I think you need, you should use database like you are doing.. – iphonic Apr 14 '17 at 06:45
  • @iphonic yes i know but it take too much load in view will appear because there are other thing also which i need to manage – Chirag Shah Apr 14 '17 at 11:06

1 Answers1

3

You can use NSNotificationCenter!

Every time in your ViewWillAppear do like,

   [[NSNotificationCenter defaultCenter] removeObserver:self name:@"EventChangeOfOrganizer" object:nil];


   [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(eventChangeOfOrganizer:)
                                             name:@"EventChangeOfOrganizer"
                                           object:nil];

and from your details screen where event change,

   dispatch_async(dispatch_get_main_queue(), ^{

                     [[NSNotificationCenter defaultCenter] postNotificationName:@"EventChangeOfOrganizer" object:nil];

                 });

Swift :

It should be something like below in swift,

    NSNotificationCenter.defaultCenter().removeObserver(self, name: "EventChangeOfOrganizer", object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("eventChangeOfOrganizer:"), name:  "EventChangeOfOrganizer", object: nil)

    dispatch_async(dispatch_get_main_queue()) { 

        NSNotificationCenter.defaultCenter().postNotificationName( "EventChangeOfOrganizer", object: nil)
    }
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75