Search code examples
iphoneobjective-cioscocoa-touchnsnotificationcenter

performSelectorOnMainThread over Notifications


I have some basic doubt's, guess someone will help me out.

Please refer this Question : Update ULabel immediately while downloading files

I have tried using performSelectorOnMainThread , which is calling the updateProgress method in another class and but the label is not updating.

But now I have used the notification like

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateProgress" object:nil userInfo:nil];

which seems to be calling the method and also updating the UILabel. Eventhough my problem is resolved, I want to know why the above performSelectorOnMainThread didn't worked out for me? Any specific reasons ?


Solution

  • The performSelectorOnMainThread can be used to run some codes in your main Thread. It not seems to be a method to call a method on another class (Even though you can call the method on another class using this if you have the working instance of the class). If you are using API calls in one of your class, you may have to use seperate threads for performing the API calls as it blocks the main thread (Its not kind to users who use your app). So in the ios you must call the UIKit from main thread only.

    The NSNotification is used to get a event call. I mean it notifies the observer when occurs a specific event that the observer registerd to be get notified.

    Hope this helps you.